Esiste un'istruzione if per verificare se un post nel loop è l'ultimo?

11 ago 2011, 03:29:00
Visualizzazioni: 15.9K
Voti: 10

Ad esempio, all'interno del loop potrei fare qualcosa come questo:

if (lastpost) { 
}
else {
}
0
Tutte le risposte alla domanda 4
2
36
if ($wp_query->current_post +1 == $wp_query->post_count) {
    // questo è l'ultimo post
}

Cambia $wp_query con la tua variabile di query se hai creato un nuovo oggetto WP_Query.

11 ago 2011 04:27:13
Commenti

ottimo, questo porterà il post più vecchio in un loop. E se volessi recuperare il post più recente?

Karue Benson Karue Karue Benson Karue
24 giu 2020 11:36:49

Allora scrivi la tua query per recuperare solo il post più recente.

Otto Otto
26 giu 2020 16:55:44
0

Ho preparato un piccolo esempio rapido per te. Dovrebbe spiegare come ottenere il primo e l'ultimo post in un loop di WordPress.

    $post_count = 0;
    $total = count($posts);

    while (have_posts()) : the_post();

        if ($post_count == 1 AND $post_count !== $total)
        {
            // Questo è il primo post
        }

        if ($post_count == $total)
        {
            // Questo è l'ultimo elemento
        }

        $post_count++;

    endwhile;
11 ago 2011 04:04:36
0
if (!get_next_post_link()) { 
    echo 'ultimo articolo qui'; 
}
14 lug 2019 18:55:38
0
if (!get_previous_post_link()) { 
    echo 'ultimo articolo qui'; 
}

OPPURE

if (get_next_post_link()) { 
    echo 'ultimo articolo qui'; 
}
25 apr 2020 13:22:48