Apakah ada pernyataan if yang dapat menentukan apakah sebuah postingan dalam loop adalah postingan terakhir?

10

Misalnya, di dalam loop saya bisa melakukan sesuatu seperti ini

if lastpost { 
}
else {
}
Carson
sumber

Jawaban:

27
if ($wp_query->current_post +1 == $wp_query->post_count) {
    // this is the last post
}

Ubah $ wp_query ke variabel kueri Anda sendiri jika Anda membuat objek WP_Query baru.

Otto
sumber
3

Saya telah memberikan sedikit contoh cepat untuk Anda. Harus menjelaskan cara mendapatkan posting pertama dan terakhir dalam lingkaran WP.

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

    while (have_posts()) : the_post();

        if ($post_count == 1 AND $post_count !== $total)
        {
            // This is the first post
        }

        if ($post_count == $total)
        {
            // This is the last item
        }

        $post_count++;

    endwhile;
Dwayne Charrington
sumber
1
if (!get_next_post_link()) { 
    echo 'the last post here'; 
}
pengguna315338
sumber
0
if (!get_previous_post_link()) { 
    echo 'the last post here'; 
}

ATAU

if (get_next_post_link()) { 
    echo 'the last post here'; 
}
iranimij
sumber