【问题标题】:WordPress: Show all posts from specific categoryWordPress:显示特定类别的所有帖子
【发布时间】:2014-04-26 15:58:05
【问题描述】:

我需要在 WordPress 中显示指定类别的最新帖子。

类别是“新闻世界”,我正在尝试此代码,但显示所有类别的所有帖子。

$wp_query = new WP_Query( array(
                'post_type' => 'post',
                'paged' => $paged
            ) );

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    这样做:

    $wp_query = new WP_Query( array(
                'post_type' => 'post',
                'paged' => $paged,
                'category_name' => 'News World' 
            ) );
    

    根据this reference。请注意,您不应使用类别名称。您需要类别 SLUG。因此,请确保您的类别 News World 有 slug“News World”。

    【讨论】:

      【解决方案2】:
      <?php $custom_query = new WP_Query('cat=9'); //your category id 
      while($custom_query->have_posts()) : $custom_query->the_post(); ?>
      
          //loop items go here
      
      <?php endwhile; ?>
      <?php wp_reset_postdata(); // reset the query ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-24
        • 2013-11-01
        相关资源
        最近更新 更多