Define the Post Excerpt Length in WordPress

  • iftekhar
  • Aug 2, 2014
  • 0 comment
  • 2451 Views
  • Print

Two very important function in WordPress is the_content and the_excerpt. Naturally, both of them has their own purpose to serve. As you can guess, excerpt would show a fraction of your entire post and on the other hand “the_content” displays the entire post.

The excerpt for a post could be automatic or a manual one. In WordPress, when you are writing a post, there is a way to add manual excerpt for a post (from Excerpt meta box). If your theme’s archive.php file uses the_excerpt function within the loop, it would automatically display the first 55 character of your post unless you are typing the manual excerpt from the post editing window.

However, it makes more sense to use the_excerpt on post archive page or on search result page. If 55 word on your post excerpt sounds overwhelming, there is a way to control the number of words that’s being displayed.

Using Filter

This is most probably the simplest way to control the post excerpt length. Let’s take a look at the code.

<?php
function mypost_excerpt_length($length) {
   return 25; }
   add_filter('excerpt_length','mypost_excerpt_length', 999);
?>

All we are doing here is, creating my own custom function which returns a numerical value or the number of word that we want to swho as the automatic excerpt from the archive page. This function gets filtered through excerpt_length hook. You can change the number of word from the function for sure.

At this point, all you need to do is to copy and paste the snippet mentioned above on your theme’s functions.php file and update the page. That should get your job done.

Note: This method works for Auto Excerpt mode only. However, if you are writing excerpt (using “Excerpt” meta box from post editing window) manually, this method won’t work.

Without Filter

Pretty much the same result can be achieved with a different approach. Like before, we would be creating a custom function and save it on our theme’s functions.php file. Here is the code.

<?php
function my_excerpt_length($string,$word_limit) { 
   $words = explode(' ', $string, ($word_limit + 1)); 
   if(count($words) > $word_limit) array_pop($words); 
   return implode(' ', $words); }
?>

Now, we have to go back to the archive.php page and paste the following line replacing “the_excerpt” function.

<?php 
$excerpt = get_the_excerpt(); 
echo my_excerpt_length($excerpt,25); 
?>

This should also limit the number of word for your post excerpt. You can easily change the number of word you want to display.

Note: This function would limit the number of words for manual excerpt as well. So, you can pretty much use the one that fits your needs. Hope you found this post to be useful.

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.