0

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

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,尽管我知道面试类别中的帖子要多得多。

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

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

4

1 回答 1

0

看起来您依赖于主查询,默认情况下每页只有 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()不需要检查,因为查询只会让您获得来自“视频”类别的帖子

于 2013-02-18T23:40:09.623 回答