WordPress: Show all posts from specific category

喜欢而已 提交于 2019-12-11 18:48:19

问题


I Need to show latest post from a specified category in WordPress.

The category is " News World ", and I am try this code but show me all post from all category.

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

回答1:


<?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 ?>



回答2:


Do this:

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

As per this reference. Note that you should NOT use the category name. You need the category SLUG. So make sure your category News World has slug "News World".



来源:https://stackoverflow.com/questions/23313267/wordpress-show-all-posts-from-specific-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!