Wordpress: How to obtain different excerpt lengths

2019-08-23 08:14发布

问题:

The length of the excerpt in wordpress is 55 words by default.

I can modify this value with the following code:

function new_excerpt_length($length) {
    return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

So, the following call will return just 20 words:

the_excerpt();

But I can't figure out how could I add a parameter to obtain different lengths, so that I could call, for example:

the_excerpt(20);

the_excerpt(34);

Any ideas? Thanks!

回答1:

uhm, answering me again, the solution was actually quite trivial. it's not possible, as far as i know, to pass a parameter to the function my_excerpt_length() (unless you want to modify the core code of wordpress), but it is possible to use a global variable. so, you can add something like this to your functions.php file:

function my_excerpt_length() {
global $myExcerptLength;

if ($myExcerptLength) {
    return $myExcerptLength;
} else {
    return 80; //default value
    }
}
add_filter('excerpt_length', 'my_excerpt_length');

And then, before calling the excerpt within the loop, you specify a value for $myExcerptLength (don't forget to set it back to 0 if you want to have the default value for the rest of your posts):

<?php
    $myExcerptLength=35;
    echo get_the_excerpt();
    $myExcerptLength=0;
?>


回答2:

There is no way to do this as far as I have found using the_excerpt().

There is a similar StackOverflow question here.

The only thing I have found to do is write a new function to take the_excerpt()'s place. Put some variation of the code below into functions.php and call limit_content($yourLength) instead of the_excerpt().

function limit_content($content_length = 250, $allowtags = true, $allowedtags = '') {
    global $post;
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    if (!$allowtags){
        $allowedtags .= '<style>';
        $content = strip_tags($content, $allowedtags);
    }
    $wordarray = explode(' ', $content, $content_length + 1);
    if(count($wordarray) > $content_length) {
        array_pop($wordarray);
        array_push($wordarray, '...');
        $content = implode(' ', $wordarray);
        $content .= "</p>";
    }
    echo $content;
}

(Function credit: fusedthought.com)

There are also "advanced excerpt" plugins that provide functionality like this you can check into.



回答3:

Thanks for your answer, thaddeusmt.

I am finally implementing the following solution, which offers a different length depending on the category and a counter ($myCounter is a counter within the loop)

/* Custom length for the_excerpt */ 
function my_excerpt_length($length) {
    global $myCounter;

    if (is_home()) {
        return 80;
    } else if(is_archive()) {
        if ($myCounter==1) {
            return 60;
        } else {
            return 25;
        }
    } else {
        return 80;
    }
}
add_filter('excerpt_length', 'my_excerpt_length');


标签: wordpress