i'd like to list my 10 most recent posts but from certain categories only - anyone can tell me how to do this?
thanks
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityi'd like to list my 10 most recent posts but from certain categories only - anyone can tell me how to do this?
thanks
simply add this above your loop
$args = array(
'posts_per_page' => 10,
'category__in' => array( 2, 6 ), //change and add the category ids here
'orderby' => 'date',
'order' => 'ASC')
query_posts($args);
and you can read more about query_posts Parameters http://codex.wordpress.org/Function_Reference/WP_Query#Parameters
By popular demand :) here is another example to doing the same but using tax_query
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('category1','category2') ////change and add the category slugs here
)
)
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'ASC')
query_posts($args);
tax_query
using slugs .. ;)
– t31os
Mar 8 '11 at 12:31
Have a look at the category query parameters here:
http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters
The easiest way to do this is to have use the category name in the query before the loop starts.
<?php query_posts($query_string . '&category_name='Your Category, Another Category'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I haven't tested it but should work.
Also check out http://codex.wordpress.org/Function_Reference/query_posts