I'm building a site built on a masonry theme and would like to add a specific category of post ('Sponsors' in this case) after every 3rd post. An example would go:
Post
Post
Post
Sponsor
Post
Post
Post
Sponsor
Post
Post
Post
Sponsor
I've searched quite abit for a similar problem but the closest I can find is for placing an ad such as below. However I'd like to replace the ad with a category post.
<?php
$count=0;
// Begin loop
while ( have_posts() ) : the_post();
$count++;
get_template_part( 'content', get_post_format() );
if ( $count%3==0 ) {
echo 'AD';
}
endwhile; ?>
I'm still a total beginner in programming (especially php and wp) but I have a few ideas of how it could be accomplished. I've tried many different ways but I'm basically shooting in the dark. This is the basic idea of what I've tried ("Sponsors" is cat=22).
<?php $count=0;
while ( have_posts() ) : the_post();
count++;
if ($count%3==0) {
<?php query_posts($query_string . '&cat=22&posts_per_page=1'); ?>
get_template_part( 'content', get_post_format() );
else
<?php query_posts($query_string . '&cat=-22&posts_per_page=1'); ?>
get_template_part( 'content', get_post_format() );
?>
This obviously doesn't work (query_posts shouldn't be used in a loop?) but I hope it shows some of my thought process. I haven't been able to find a way to exclude a category from a loop already running.
Another idea I had would be 2 loops where the first loop excludes the "Sponsor" category and posts 3 times and then a second loop which only posts 1 post from "Sponsor". However when I do this, I don't know how to make this cycle repeat itself (ie. only 4 posts show up).
How would I go about doing this? I appreciate the help!
query_posts
. UseWP_Query