Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a jQuery slider at the top of my index.php, which works fine. The only problem is that I would like to use query_posts to only display the latest post from five different categories, and always show one from each.

So when a new post is created in, lets say the category interview, this will be displayed. But the interview post which was posted one day earlier will then not be displayed, but the latest post from the four other categories are still there.

I think this is a bit complicated, no?

share|improve this question
    
Don't use query_posts() for a secondary loop, such as a posts slider, especially on index.php. Using query_posts() is intended only for the primary Loop. Use get_posts() or WP_Query() instead, for secondary loops. –  Chip Bennett Oct 27 '11 at 15:27
    
Thanks for the comment Chip! What can this cause? Extra load on the database? –  Mats Nov 10 '11 at 20:24
    
add comment

1 Answer

i think you'd have to use get_posts 5 different times. what about this:

global $post;
$posts = array();

//categories you want to pull latest posts from;
$cats = (1,2,3,4,5);

foreach($cats as $cat):
$args = array( 'numberposts' => 1, 'category' => $cat ); 
$posts[] = get_posts($args);
endforeach;

if($posts): ?>
<ul>
<?php foreach( $posts as $post ) :  setup_postdata($post); ?>
   <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

i totally didn't test this, but I think it might work

share|improve this answer
    
Hmm, yeah, maybe. I'll have to play around a bit. Thanks for the help. –  Mats Sep 4 '11 at 15:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.