I have been trying for a few days to make a homepage that displays all my posts, and after every 2 posts, a testimonial.
I have found dozens of articles describing how to count while you are in the loop and place something every 'n' number of times. Thats no problem, but what is a problem, is running Loop A, and then every 'n' number of times, placing the next item from Loop B. WP wants you to reset one loop before starting another.
So, I need to query each set separately, parse the queries, and build a new one in the order I want it before I pass to the loop. The problem is,
WP_Query objects are not arrays. So i can't figure out how to do what I want. I have the whole thing worked out except for one line.
I have deferred to writing the line as I would for arrays so it is evident what I am trying to do.
<?php $testimonials = new WP_Query( array('fields'=>'ids', 'post_type'=>'testimonial', 'posts_per_page' => -1)); ?> <?php $others = new WP_Query( array('fields'=>'ids', 'post_type'=>'post', 'posts_per_page' => -1)); ?> <?php $combined = new WP_Query() ?> <?php $t = 0; $o = 0; ?> <?php $total_t = $testimonials->post_count; $total_o = $others->post_count; ?> <?php while ($total_t > $t && $total_o - 1 > $o ): ?> <!-- PROBLEM LINE --> <?php array_push($combined, $testimonials[$t], $others[$o], $others[$o+1]); ?> <!-- /PROBLEM LINE --> <?php $t++; $o = $o + 2; ?> <?php endwhile; ?>