使用过滤器循环浏览 Wordpress 帖子in_category并不能获得所有帖子



我正试图将特定类别中的一些帖子放入多维数组中,如下所示:

wp_reset_query();
query_posts();
while (have_posts()) : the_post();
    if (in_category('videos')) {
        $postAttribute["permalink"] = get_permalink();
        $postAttribute["image_url"] = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
        $postAttribute["title"] = get_the_title();
        $postAttribute["comments_number"] = get_comments_number();
        array_push($videos, $postAttribute);
    };
endwhile;

然后检查我运行的阵列:

echo count($videos);

因此,我一直得到2分,尽管我知道面试类的帖子比这多得多。

我检查了帖子的最大数量设置,把它设置得更高只是为了看看,但仍然一无所获。

知道我可能错过了什么吗?

看起来你依赖于主查询,默认情况下,每页只会收到20篇左右的帖子。

改为执行自定义查询:

$my_query = new WP_Query(array(
  'posts_per_page' => -1, // all
  'category_name' => 'videos',
));
while($my_query->have_posts()){
  $my_query->the_post();
  // do your thing here
}

不需要in_category()检查,因为查询将只获得来自"视频"类别的帖子

相关内容

最新更新