Come fare in modo che il filtro excerpt_more si applichi all'effettivo estratto del post?

17 giu 2011, 22:26:51
Visualizzazioni: 5.8K
Voti: 1

Nella funzione get_blog_excerpt() qui sotto, il filtro excerpt_more funziona perfettamente quando l'estratto del post non è presente, tuttavia, quando il post ha un estratto, non ottengo il link "Leggi tutto".

Capisco che the_excerpt prima controlla se è presente un estratto del post, il che va bene, ma voglio che anche ad esso venga applicato il link "Leggi tutto".

Cosa devo cambiare per fare in modo che excerpt_more si applichi in tutti i casi?

function get_blog_excerpt(){
    add_filter('excerpt_length', 'ce4_excerpt_length');
    add_filter('excerpt_more', 'ce4_excerpt_more');
    return the_excerpt();
}

function ce4_excerpt_length($length) {
    return 150;
}

function ce4_excerpt_more($more) {
    global $post;
    return '...<a href="'. get_permalink($post->ID) . '">Leggi tutto</a>';
}


function get_blog_links(){
    global $post;
    setup_postdata($post);
    $myposts = get_posts($args);echo '<div id="menuFooterRecent" class="blog">'; 
    echo '<ul>'; 
    foreach($myposts as $idx=>$post){ ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php 
    echo get_blog_excerpt();
    echo '<div style="clear:both">&nbsp;</div>';?></li>
    <?php } echo '</ul></div>';
}

Il codice sopra si trova all'interno di functions.php

Il codice qui sotto si trova all'interno di archive.php

<?php 
if(is_category()){
    if (get_query_var('cat') == get_category_by_slug('blog')->term_id){
        get_blog_links();
    }
    else 
    {
    get_category_links();
    }
} ?>    
4
Commenti

Dove viene chiamato (o agganciato) get_blog_excerpt()?

Chip Bennett Chip Bennett
17 giu 2011 22:33:25

Da dentro una funzione in functions.php

Scott B Scott B
17 giu 2011 23:23:58

Potrebbe essere causato da un plugin. Ho avuto esattamente lo stesso problema, il mio codice in functions.php per modificare la lunghezza e l'aspetto dell'estratto va bene (testato in un'installazione pulita di wp) ma è stato "ucciso" da un plugin, nel mio caso 'Event Calendar 3.1.4'. Tutto funziona bene se disattivo il plugin. Prova a dare un'occhiata in questo modo.

kevin kevin
21 lug 2011 18:53:59

Risolto il mio problema utilizzando questo plugin: http://wordpress.org/extend/plugins/advanced-excerpt/

kevin kevin
21 lug 2011 19:00:56
Tutte le risposte alla domanda 1
3

Suppongo che tu stia chiamando get_blog_excerpt() da qualche parte nel tuo template?

Se è così, cosa succede se chiami semplicemente the_excerpt() e poi estrai le due chiamate add_filter() dalla funzione contenitore? Cioè functions.php avrebbe questo aspetto:

function ce4_excerpt_length($length) {
    return 150;
}
add_filter('excerpt_length', 'ce4_excerpt_length');

function ce4_excerpt_more($more) {
    global $post;
    return '...<a href="'. get_permalink($post->ID) . '">Continua a leggere</a>';
}
add_filter('excerpt_more', 'ce4_excerpt_more');

E nel tuo template, dovresti semplicemente chiamare the_excerpt().

Se questo funziona, allora sospetto che il problema sia che i tuoi filtri non vengono applicati - probabilmente perché sono racchiusi nella funzione contenitore.

17 giu 2011 22:38:07
Commenti

Chip, grazie per il suggerimento. Ma spostare i filtri al di fuori del contenitore non sembra avere alcun effetto sullo script. Funziona allo stesso modo in ogni caso.

Scott B Scott B
17 giu 2011 23:29:23

get_blog_excerpt viene chiamato da una funzione all'interno di functions.php, che a sua volta viene chiamata da una funzione al di fuori del loop in archive.php

Scott B Scott B
17 giu 2011 23:30:32

Quindi, hai provato semplicemente a chiamare the_excerpt()?

Chip Bennett Chip Bennett
18 giu 2011 00:20:57