Summary of Wordpress Posts

Asked

Viewed 147 times

3

I need the summary size of the posts in my Frontpage to be different from the summary size of the page where all 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 summary has 15 words on all pages, however I need it in frontpage.php be 15 words, and in home.php have 100 words.

2 answers

2

When you create an action for a given hook, within this function there can 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' );
  • I tried to do this way, but this always falling in the second "if ( is_home() )" getting on both pages 100 words... my code is exactly like yours, just fixed the function is_frontpage() to is_front_page(), because it was giving an undefined function error.

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 limit the characters like this:

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

This way the summary in Frontpage will always have 120 characters while the summary of the posts page continues with the standard 55 words.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.