Multiple excerpt lengths in wordpress

2020-01-26 18:51发布

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

15条回答
Fickle 薄情
2楼-- · 2020-01-26 19:39

You can add to your functions.php file this function

function custom_length_excerpt($word_count_limit) {
    $content = wp_strip_all_tags(get_the_content() , true );
    echo wp_trim_words($content, $word_count_limit);
}

Then call it in your template like this

<p><?php custom_length_excerpt(50); ?>

The wp_strip_all_tags should prevent stray html tags from breaking the page.


Documentation on functions

查看更多
beautiful°
3楼-- · 2020-01-26 19:39

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:

<?php
function wp_trim_excerpt($text = '') {
    global $excerpt_length;
    $len = $excerpt_length > 0 ? $excerpt_length : 55;
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]&gt;', $text);
        $excerpt_length = apply_filters('excerpt_length', $len);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[&hellip;]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    }
    $excerpt_length = null;
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

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:

<?php $excerpt_length = 10; the_excerpt() ?>

Changing core is a horrible solution so I'd love to know if someone comes up with something better.

查看更多
放我归山
4楼-- · 2020-01-26 19:40

I would do like this :

function _get_excerpt($limit = 100) {
    return has_excerpt() ? get_the_excerpt() : wp_trim_words(strip_shortcodes(get_the_content()),$limit);
}

Usage :

echo _get_excerpt(30); // Inside the loop / query

Why ?

  • If has_excerpt should return given excerpt
  • It not, So trim words / strip shortcodes from the_content
查看更多
Luminary・发光体
5楼-- · 2020-01-26 19:43

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.

查看更多
劫难
6楼-- · 2020-01-26 19:45

I found a great plugin that can do this -- Content and Excerpt Word Limit

查看更多
来,给爷笑一个
7楼-- · 2020-01-26 19:45

Use Advanced Excerpt
http://wordpress.org/extend/plugins/advanced-excerpt/ plugin. I too found an answer from this page.

查看更多
登录 后发表回答