Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple excerpt lengths in wordpress

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') ?> 

Cheers, Dave

like image 984
davebowker Avatar asked Nov 02 '10 22:11

davebowker


People also ask

How do I create an excerpt in WordPress?

To add the excerpt on your blog post, go to Posts >> Add New or simply edit your existing post. Now, on the right options panel, click on the Excerpt option and add the excerpt for your posts. Once done, click on Publish/Update button to save your changes.

How do I find excerpts in WordPress?

In the right-hand panel of the WordPress content editor, you should see the 'Excerpt' dropdown. Click the downward arrow next to it. It'll expand to show the excerpt box. You can type your custom post excerpt here.

How do I remove excerpt in WordPress?

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 ); Here, you will notice the limit of post excerpt is set to zero. Once this code is introduced in your function. php file and you successfully removed the post excerpt.


2 Answers

How about...

function excerpt($limit) {       $excerpt = explode(' ', get_the_excerpt(), $limit);        if (count($excerpt) >= $limit) {           array_pop($excerpt);           $excerpt = implode(" ", $excerpt) . '...';       } else {           $excerpt = implode(" ", $excerpt);       }        $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);        return $excerpt; }  function content($limit) {     $content = explode(' ', get_the_content(), $limit);      if (count($content) >= $limit) {         array_pop($content);         $content = implode(" ", $content) . '...';     } else {         $content = implode(" ", $content);     }      $content = preg_replace('/\[.+\]/','', $content);     $content = apply_filters('the_content', $content);      $content = str_replace(']]>', ']]&gt;', $content);      return $content; } 

then in your template code you just use..

<?php echo excerpt(25); ?> 

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

like image 130
Marty Avatar answered Sep 21 '22 13:09

Marty


As for now, you can upgrade Marty's reply:

function excerpt($limit) {     return wp_trim_words(get_the_excerpt(), $limit); } 

You can also define custom 'read more' link this 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()); } 
like image 27
Michał Rybak Avatar answered Sep 24 '22 13:09

Michał Rybak