是否有if语句可以确定循环中的帖子是否为最后一个帖子?


10

例如,在循环内我可以做这样的事情

if lastpost { 
}
else {
}

Answers:


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

如果创建了新的WP_Query对象,请将$ wp_query更改为您自己的查询变量。


3

我为您编写了一个简短的小示例。应该说明如何在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'; 
}

要么

if (get_next_post_link()) { 
    echo 'the last post here'; 
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.