WordPress Excerpt Length Customize
Generally for the Blog websites, bloggers don’t generally prefer a static page to appear on Front Page or Home page. Instead, dynamic page is chosen and it will usually list all the posts/articles sorted out based on the date posted in the decreasing order.
For each post, Post or Article Title would be displayed along with the thumbnails. So just post title wouldn’t mean anything to the user. We also need to display “few lines or words” related to the article to give a glimpse of the post to the user. This functionality is achieved by the “Excerpt” text functionality available in WordPress.
Also Read: Add Social Media Buttons (Facebook, Twitter, Google+) to WordPress Blog
Customize Excerpt in WordPress
By default, WordPress Excerpt length & content is provided by the below functionality.
- the_excerpt() function available in wordpress template file helps us to get the first 55 letters of the post content and it is usually shown along with the post title in the home page.
- It is possible to customize this default WordPress Excerpt length as per need from 55 word count.
- the_excerpt() function displays 3 dots […] at the end of the excerpt text. Instead of this ellipsis(…), we can easily display any other text with the link added to the Post.
PHP Code to Change Default Excerpt Length in WordPress
With few lines of PHP code, we will be able to customize Excerpt in WordPress. We need to add all the given code under Child Theme’s functions.php file.
Step 1: In the below code, the length is changed from the default 55 to 70. We linked the hook to invoke our newly added function new_excerpt_length() during the action trigger of excerpt_length.
function new_excerpt_length(){ return 70; } add_filter('excerpt_length', 'new_excerpt_length');
Step 2: function new_excerpt_more() helps to change the default Read more( … ) to (…[Continue Reading]) with a added hyperlink to the Post itself.
function new_excerpt_more($more) { return '... <a class="excerptMore" href="' . get_permalink($post->ID).'">[Continue Reading]</a>'; } add_filter('excerpt_more', 'new_excerpt_more');
Step 3: CSS style added for the customized hyperlink
.excerptMore{ font-weight: 500 !important; color:purple !important; }
By adding these PHP lines, we can customize Excerpt in WordPress according to our need.
More Tips: Enable PHP Code in Text Widget for WordPress Websites and Blogs
Reference: https://codex.wordpress.org/Customizing_the_Read_More