Excerpt function in wordpress

0

I have the following excerpt function in wordpress

add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

/**
    * Filter the except length to 20 words.
    *
    * @param int $length Excerpt length.
    *
    * @return int (Maybe) modified excerpt length.
*/


function wpdocs_custom_excerpt_length( $length ) {
    return 40;
}

add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

And I wonder if it is possible to have more than one excerpt function to use in different post. The purpose is to give different lengths to the posts that are shown depending where they are located

    
asked by Dario B. 13.12.2017 в 10:34
source

1 answer

2

Yes, but you need to build your own excerpt, the code that you put modifies only the length of Wordpress.

In functions.php (this code does not remember where you take it from but it is the one I use, eye counts characters not words).

function my_excerpt( $my_lenght = null ){
    if( empty( $my_lenght ) ) {
        $lenght = 200; //Default value
    } else {
        $lenght = $my_lenght;
    }

    $excerpt = get_the_content();
    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $lenght);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = $excerpt.'...';

    return $excerpt;
}

In your templates inside the loop ..

echo '<p>'. my_excerpt(100) . '</p>';

In another template ...

echo '<p>'. my_excerpt(400) . '</p>';

... etc

    
answered by 14.12.2017 / 02:52
source