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
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.
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.
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.
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(']]>', ']]>', $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/
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 »</a>'; } function excerpt($limit) { return wp_trim_words(get_the_excerpt(), $limit, custom_read_more()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With