Different Excerpt Lengths in different Pages in WordPress

When it comes to WordPress Theme Development, it is important for developers to create a unique, clean and flexible site for both front-end as well as back-end users. You may want to show readers short description of your blogs i.e. Post Excerpts on Homepage or The Front Page and want readers to click on the Title or View More links and want encourage them to continue reading article. The users just don’t read your article because of the Post Title. If they find your short post description boring, then it’s almost certain that they will run away from your site and fly to Mexico. So, choose wisely your excerpt in order to increase search traffic to your blog. In this tutorial, I am going to show you simple trick to display different excerpt lengths in different pages in WordPress. By simple trick using hook, we will be able to attain our goal. Just follow these steps precisely.

Different Excerpt Lengths in different Pages in WordPress

In WordPress, Excerpt means a short text taken from your whole blog post which gives very brief and meaningful summary about your blog post. the_excerpt(); is used  as a replacement for the_content(); to force excerpts to show within The Post Loop.

By default, the WordPress uses wp_trim_excerpt(); hook to use excerpt filter with length size of 55. Let’s suppose we want to change the excerpt length to 25 on the home page or front page. Add the following code in your functions.php file you wish to display the length of the excerpt.

function my_custom_excerpt_length( $length ) {
	return 25;
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );

Now,  if you use the_excerpt(); function in your loop, it will show 25 words in the excerpt as shown below:

ALSO READ  Change From and Email Address names in WordPress

Different Excerpt Lengths in different Pages in WordPress


However, the above code will display the excerpt length of 25 to all the pages wherever you call the the_excerpt function.

If you want to display different Excerpt Lengths in different pages in WordPress Blog Posts, you need another simple trick. You just need to modify the above code, nothing more than that. Suppose, you want to show Excerpt Length of 25 in Home Page or the Front Page and 15 excerpt length in rest of the page, then simply replace above code in your functions.php file.

This is the Part:

function different_excerpt_length( $length ) {
return (is_front_page()) ? 25 : 15;
add_filter( 'excerpt_length', 'different_excerpt_length', 999 );

Now, you may want to show  your custom Continue or View More links to be displayed at the  end of the excerpt. By default the WordPress set it to echo ‘[…]’  string just below the excerpt.

To further customization the default excerpt more string by your preferred link, just add the following code in your functions.php file.


function new_excerpt_more($more) {
global $post;
return '<br><br><a role="button" class="btn btn-default" href="'. get_permalink($post->ID) . '"> Continue</a>';
add_filter('excerpt_more', 'new_excerpt_more');

Now, if you Refresh your Homepage or Front-Page,  you’ll see Continue Button as shown in the screenshot below:

Different Excerpt Lengths in different Pages in WordPress

Alternative Option

This is one trick. You can even Create your very own Excerpt Function to display multiple excerpt length for your posts/pages.

Enter the below code in your functions.php file.

class Excerpt {
  // Default length (by WordPress)
  public static $length = 55;
  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 25,
      'medium' => 45,
      'long' => 85
  public static function length($new_length = 45) {
    Excerpt::$length = $new_length;
    add_filter('excerpt_length', 'Excerpt::new_length');

  // Tells WordPress  new length
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
      return Excerpt::$length;
  // Echoes out  excerpt
  public static function output() {
// An alias to  class
function my_excerpt($length = 45) {

Now instead of the_excerpt function, you can use the one you created above. i.e. my_excerpt function

ALSO READ  How to Create a Custom Search Form in WordPress

Insert one of the below codes in your template file where you want to display.

my_excerpt('medium'); // calls the defined Medium Excerpt length
my_excerpt('short'); // calls the defined Short Excerpt Length
my_excerpt('long'); // calls the defined Long Excerpt Length

my_excerpt(10); // 10 characters

Removing the […] default after each Excerpt Text

By default, the excerpt comes with [...] after every excerpt text.

To remove the [...] after the excerpt content, you can add the following in your functions.php.

function remove_excerpt_default( $more ) {
  return '';
add_filter('excerpt_more', 'remove_excerpt_default');

This is it.  We’ve successfully added different excerpt lengths in different pages. If you have any query or question, just comment the form below. I will try to reach to you ASAP.

Credits: StackOverflow