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
You can add to your functions.php file this function
Then call it in your template like this
The
wp_strip_all_tags
should prevent stray html tags from breaking the page.Documentation on functions
I know this is a really old thread, but I just struggled with this problem and none of the solutions I found online worked properly for me. For one thing my own "excerpt_more"-filter was always cut off.
The way I solved it is ugly as hell, but it's the only working solution I could find. The ugliness involves modifying 4 lines of WP core(!!) + the use of yet another global variable (although WP already does this so much I don't feel too bad).
I changed
wp_trim_excerpt
in wp-includes/formatting.php to this:The only new stuff is the
$excerpt_length
and$len
bits.Now if I want to change the default length I do this in my template:
Changing core is a horrible solution so I'd love to know if someone comes up with something better.
I would do like this :
Usage :
Why ?
has_excerpt
should return given excerptthe_content
Be careful using some of these methods. Not all of them strip the html tags out, meaning if someone inserts a link to a video (or url) in the first sentence of their post, the video (or link) will show up in the excerpt, possibly blowing up your page.
I found a great plugin that can do this -- Content and Excerpt Word Limit
Use Advanced Excerpt
http://wordpress.org/extend/plugins/advanced-excerpt/ plugin. I too found an answer from this page.