0

I am attempting to control a loop I have within a loop. What I would like to do is display all post and have a custom post type inserted every 3 post. So it would be like this "post post post CPT post post post cpt..." I accomplished this by doing this

global $loop_counter;
$loop_counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
?>

// DO STUFF

<?php $loop_counter++; ?>

if ( $loop_counter%3 == 0 ) { 
    $args = array(
        'post_type' => 'testimony',
    );
    $testimony = new WP_Query($args);
    if ( $testimony->have_posts() ) {
        while ( $testimony ->have_posts() ) {
            $testimony->the_post();
            ?>

            //LOOP WITHIN LOOP

        } } 
    } END loop count if

?>

I slimmed down the code a bit but this should be enough. Now this is working like I want with one issue. The inner loop keeps repeating as long as the main loop has posts. I want the stop looping through the inner loop as soon as we are out of posts to display. Any help would be appreciated.

1 Answer 1

1

I think you want something more like this:

$args = array(
  'post_type' => 'testimony',
);
$testimony = new WP_Query($args);

if ( have_posts() ) {
  while ( have_posts() ) {
    the_post(); 
    if ( 0 !== $wp_query->current_post 
      && 0 === $wp_query->current_post%3 
      && $testimony->have_posts() 
    ) {
        $testimony->the_post();
        echo 'inner-loop';
        the_title();
        echo 'inner-loop';
        echo '<br>';
    }
    wp_reset_postdata();
    the_title(); 
    echo $wp_query->current_post;
    echo '<br>';
  }
}

You don't need the loop counter. There is a counter built into WP_Query, and you don't need to be running that $testimony query at every iteration of the outer loop.

The the_post method will increment the Loop by one, so at each iteration the inner loop runs that, echos content and moves on.

There is obviously some debugging code in there. You can edit that out and replace it whatever you want.

5
  • I cannot get this to work. I have tried this several different ways with no luck. The simplist version was copy and paste the code and that retrieved only testimony posts. I was not calling $testimony outside of the inner loop before. Commented Jul 16, 2013 at 2:31
  • I know you weren't pulling the $testimony outside the Loop but why would you run it inside? The parameters don't change. The code works fine when I run it. The problem must have something to with the context, but I am not sure what.
    – s_ha_dum
    Commented Jul 16, 2013 at 3:10
  • Are you using your above within another loop or is this the whole loop? Commented Jul 16, 2013 at 4:20
  • This is the whole thing. The outer Loop depends upon the main query, just as your original code does. There is no inner Loop technically but it does walk through the $testimony Loop nonetheless because $testimony->the_post(); runs every third post of that outer Loop. It should be a drop-in replacement for the code you posted. Also, I made a small edit to the function.
    – s_ha_dum
    Commented Jul 16, 2013 at 4:32
  • I appreciate your help but I just cant get it to work. This is a Genesis Child theme but I removed the loop and am attempting to get this custom loop to work. It is now calling on both loops but the testimony are displaying randomly awesomescreenshot.com/04e1iet157 as you can see it is loading in a pattern 12122(repeated). Commented Jul 16, 2013 at 5:13

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.