WordPress Posts Summary

3

I need the post summary size on my frontpage to be different from the summary size of the page where all the posts are displayed ( home.php ).

In my file functions.php I put the following code:

//adiciona um novo tamanho de resumo de posts
function novo_tamanho_do_resumo($length) {
    return 15; //15 palavras
}
add_filter('excerpt_length', 'novo_tamanho_do_resumo');

The problem with this code is that the abstract has 15 words on all pages, however I need it in frontpage.php to be 15 words, and that in home.php has 100 words.

    
asked by anonymous 13.10.2016 / 21:02

2 answers

0

To solve this problem I did so:

I created the following function in the functions.php file

function cropText($texto, $limite){
    if (strlen($texto) <= $limite) echo $texto;
    echo array_shift(explode('||', wordwrap($texto, $limite, '||'))) . " [...]";
}

and in my frontpage.php I call the content of the post and I limit the characters like this:

<?php cropText(get_the_content(), 120); ?>

In this way the summary on the frontpage will always be 120 characters while the summary of the posts page continues with the 55 word standard.

    
14.10.2016 / 20:17
2

When you create an action for a particular hook, within this function there may be checks for what you need and return the value accordingly.

In your case you can do:

//adiciona um novo tamanho de resumo de posts
function novo_tamanho_do_resumo ( $length ) {

    if ( is_frontpage() ) { // ou qualquer outro if pra verificar em qual página está

        return 15; //15 palavras

    } else if ( is_home() ) {

        return 100; //100 palavras

    } else {

        return 55; //valor padrão

    }

}
add_filter( 'excerpt_length', 'novo_tamanho_do_resumo' );
    
13.10.2016 / 22:19