Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

As it says in the title, I'm looking for multiple excerpt lengths in WordPress.

I understand you can do this in functions.php:

function twentyten_excerpt_length( $length )

{

return 15;

}

add_filter( 'excerpt_length', 'twentyten_excerpt_length' );

What I want to know is how you can have multiple of these each returning different numerical values so I can get short excerpts for sidebar loops, longer excerpts for featured loops, and the longest excerpt for the main article.

Something like using these in the templates:

<?php the_excerpt('length-short') ?>

<?php the_excerpt('length-medium') ?>

<?php the_excerpt('length-long') ?>

1 Answer

0 votes
by (106k points)

For multiple excerpt lengths in Wordpress you can use the following code:

function excerpt($limit)

{

return wp_trim_words(get_the_excerpt(), $limit);

} 

You have another option where you can also define custom 'read more' link by the following way:-

function custom_read_more()

{

return '... <a class="read-more" href="'.get_permalink(get_the_ID()).'">more&nbsp;&raquo;</a>';

}

function excerpt($limit)

{

return wp_trim_words(get_the_excerpt(), $limit, custom_read_more());

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 6, 2019 in Web Technology by Sammy (47.6k points)
+1 vote
1 answer
0 votes
1 answer

Browse Categories

...