在WordPress中,我们可以使用have_posts()
函数来判断是否有文章要显示。下面是一个示例代码,它会列出除了最后4篇文章以外的所有文章:
4,
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids'
));
// 定义主查询
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => $exclude_posts
);
$main_query = new WP_Query($args);
// 循环输出文章
if ($main_query->have_posts()) :
while ($main_query->have_posts()) : $main_query->the_post();
// 输出文章标题和内容
the_title('', '
');
the_content();
endwhile;
endif;
// 重置文章查询
wp_reset_postdata();
?>
在上面的代码中,我们首先使用get_posts()
函数获取最后4篇文章的ID,并将这些文章的ID存储在$exclude_posts
数组中。然后,我们定义了一个主查询$args
,使用post__not_in
参数来排除这些文章。接下来,我们使用WP_Query
来执行查询,并使用have_posts()
和the_post()
来循环输出文章的标题和内容。
最后,我们使用wp_reset_postdata()
来重置文章查询,以防止与其他可能的查询冲突。