如何从WP_Query循环内获取下一个帖子 ID


How to get Next post ID from inside the WP_Query loop?

我创建了一个如下所示的wordpress代码,以列出特定术语下的所有帖子。

<?php
$args = array( 
    'post_type' => 'videos', 
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'video-categories',
            'field' => 'id',
            'terms' => $category_id
        )
    )
);
?>
<?php   $loop = new WP_Query( $args ); ?>
<?php   if($loop) :?>
<?php   while ( $loop->have_posts() ) : $loop->the_post();?>
... things to do inside the loop ...
<?php   endwhile; ?>
<?php   endif;?>
<?php   wp_reset_postdata();?>

我想要实现的是,我想从循环内部获取每个帖子的下一篇文章的 ID。我该怎么做?例如,在循环下,如果当前 ID 为 10,那么我需要帖子 ID 10 的下一个帖子 ID。

据我所知,您想循环获取下一篇文章 id 的问题。为此:您可以使用函数:get_next_post()

get_next_post()

在循环中使用$wp_query->is_single = true;

如果你是单身.php你可以这样做:

$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous)