1

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!

  • 3
    Have you tried something already? It would be nice if you show us the code you have tried, working or not, to avoid the "Do-the-job-for-me" format. – cybmeta May 13 '14 at 17:36
  • I appreciate the response @cybnet. I edited the post with more information. – tensixtythree May 14 '14 at 23:14
  • You could hook in a widget and add a widget for posts in one category. – Brad Dalton May 15 '14 at 2:05
  • I forgot to add this in my answer. Don't use query_posts. Use WP_Query – Pieter Goosen May 15 '14 at 7:38
1

You can use this as a start. This need to go into functions.php. You can use your normal loop in your template files, no need to change anything there. This will add a post from a category after post 3 and 6. Just remember to change the category name with the slug of your category.

function category_after_third_post( $post ) {
        global $wp_query;

        if ( $wp_query->post != $post )
            return;

        if ( 3 != $wp_query->current_post || 6 != $wp_query->current_post )
            return;

        $args = array(
        'category_name' => 'uit-die-koskas',
        'posts_per_page' => 1
        );

        $catquery = new WP_Query($args);

        if ( $catquery->have_posts() ) :

            while ( $catquery->have_posts() ) : $catquery->the_post();

                get_template_part( 'content', get_post_format() );

            endwhile;

        endif; 
    }

add_action( 'the_post', 'category_after_third_post' );

Alternatively, you can create a widget and add that after every 3rd post.

function cat_posts_sidebar() {
    register_sidebar( array(
        'name' => __( 'Sidebar for ads', 'my-theme' ),
        'id' => 'sidebar-10',
        'description' => __( 'Sidebar to display cat post', 'my-theme' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );    
}
add_action( 'widgets_init', 'cat_posts_sidebar' );

Then modify the first function to this.

function category_after_third_post( $post ) {
        global $wp_query;

        if ( $wp_query->post != $post )
            return;

        if ( 3 != $wp_query->current_post || 6 != $wp_query->current_post )
            return;

        echo '<div class="after-post-widget widget-area">';
             dynamic_sidebar( 'sidebar-10' );
        echo '</div><!-- end .after-post-widget -->';
    }

add_action( 'the_post', 'category_after_third_post' );  
  • +1 but wouldn't you add a category conditional for the widget? – Brad Dalton May 15 '14 at 4:55
  • Yes, you would. Was in a hurry, on my way to work, when I posted. As said, this is a starting point for the OP. – Pieter Goosen May 15 '14 at 4:58

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.