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

时间:2013-02-18 23:33:53

标签: php arrays wordpress loops multidimensional-array

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

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,尽管我知道有更多的帖子比面试类别更多。

我检查了最大帖数设置,将其设置得更高只是为了看,但仍然没有任何结果。

知道我可能缺少什么吗?

1 个答案:

答案 0 :(得分: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()检查不是必需的,因为查询只会从“视频”类别中获取帖子