Have you tried printing out $wp_query to see which query parameters WordPress is using?
Add something like the following to your code, save, and then view source.
$args = array( 'category__and' => array(3,4,5,6,7), 'posts_per_page' => 1);
query_posts( $args );
<!-- wp_query = <?php print_r($wp_query); ?> -->
I’d also consider using WP_Query instead of query_posts since you won’t have to mess with the original query
display the last post from each parent category listed in array
the code will only show just one post that has all these categories at the same time;
i.e. this post to show must have the categories 3,4,5,6,7 assigned to it.
display the last post from each parent category listed in array
does this include any posts in a child category?
if so, you might need to run a foreach loop with the category ids
example:
$cat_array = array(3,4,5,6,7);
foreach( $cat_array as $cat_id ) :
/*THE QUERY AND LOOP HERE*/
endforeach;
and use
'cat' => $cat_id
in the query (to allow child cat posts)
or
'category__in' => $cat_id
(for posts just in the cat)
http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters
Thanks guys appreciate. Almost there 🙂 Everything is working fine, only the category name is missing. Latest Posts in ??? Category
This is my code now.
<?php
wp_reset_query();
$cat_array = array(3,4,5,6,7);
foreach( $cat_array as $cat_id ) :
$args = array(
'posts_per_page' => 1,
'category_name' => $cat_id->slug,
'cat' => $cat_id,);
query_posts($args); // reset to original
if (have_posts()) :
echo '<h2>Latest Posts in '.$cat_id->name.' Category</h2>';
?>
<?php while (have_posts()) : the_post(); ?>
<div>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h2>
</div>
<?php
if ( is_category($vidcat) ) { the_content(); }
else { echo strip_tags(get_the_excerpt(), '<a><strong>'); }
?>
<!-- this area is for the display of your posts the way you want it -->
<!-- i.e. title, exerpt(), etc. -->
<?php endwhile; ?>
<?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?>
<?php endif; wp_reset_query; ?>
<?php endforeach; ?>
$cat_id is really only the category id, not the category object.
try:
echo '<h2>Latest Posts in '. get_category($cat_id)->name .' Category</h2>';
http://codex.wordpress.org/Function_Reference/get_category