I'm using a loop to generate the slides I need for a Coda Slider slideshow. The slides should be pulling the most recent post from 5 different categories. The problem I'm having is that it's pulling the 5 most recent posts from the 5 categories collectively and not individually. So it's pulling 2 posts from the 1st category and then 1 from each of the others.

Any way to get this to work properly?

This is the code I'm using:

$cat_post_query = new WP_Query($query_string . 'cat=7,8,10,9,11');
while ($cat_post_query->have_posts()) : $cat_post_query->the_post();
$do_not_duplicate = $post->ID;?>

* slide content *

<?php endwhile;  ?>

For those who were interested in an answer to this question, I found the answer myself.

<div class="coda-slider"  id="slideshow">    

// array of category IDs
$categories =  array(1,2,3,4,5);

foreach ($categories as $cat) :
  $post = false;
  $post = get_posts('cat='.$cat.'&posts_per_page=1');
  if($post) :
    $post = $post[0];
    setup_postdata($post); ?>

    <!-- rest of normal loop -->

    <div <?php post_class(); ?>>
      <h2 class="title">title used to dynamically generate thumbs in codaslider</h2>
      <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
      <?php the_content(); ?>

  <?php endif; ?>
<?php endforeach; ?>  


The original foreach loop code came from this post:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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