3

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?

3
  • 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. Commented Oct 27, 2011 at 15:27
  • Thanks for the comment Chip! What can this cause? Extra load on the database?
    – Mats
    Commented Nov 10, 2011 at 20:24
  • See the query_posts() Codex entry regarding usage caveats. Commented Nov 10, 2011 at 23:52

1 Answer 1

1

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

1
  • Hmm, yeah, maybe. I'll have to play around a bit. Thanks for the help.
    – Mats
    Commented Sep 4, 2011 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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