问题
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