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 {
}

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.


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;


0
if (!get_previous_post_link()) { 
    echo 'the last post here'; 
}

ATAU

if (get_next_post_link()) { 
    echo 'the last post here'; 
}
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.