1

I am a bit lost and looking for guidance. I have a homepage that lists blog posts. However after every 3rd post (we'll call this the "Main Feed") I want to add a section in between that looks different and calls a specific category. After that section I want the "Main Feed" to continue.

I've only ever had to call one section at a time and really looking for some guidance. I appreciate the help greatly!

Main Feed

<?php query_posts('cat=-20'); while (have_posts()) : the_post(); ?> 
    <div>
      <h2><?php the_title(); ?></h2>
      <p><?php echo get_the_excerpt(); ?></p>
    </div>


<?php endwhile; ?>  
<?php wp_reset_query(); ?>

This Should Interrupt Main Feed Once Every 3 Posts

<?php query_posts(''cat=3&posts_per_page=2''); while (have_posts()) : the_post(); ?> 
    <div>
      <h2><?php the_title(); ?></h2>
      <p><?php echo get_the_excerpt(); ?></p>
    </div>


<?php endwhile; ?>  
<?php wp_reset_query(); ?>

This Should Interrupt Main Feed Once Every 3 Posts

<?php query_posts(''cat=4&posts_per_page=2''); while (have_posts()) : the_post(); ?> 
    <div>
      <h2><?php the_title(); ?></h2>
      <p><?php echo get_the_excerpt(); ?></p>
    </div>


<?php endwhile; ?>  
<?php wp_reset_query(); ?>

I'd like this repeating pattern where specific categories are inserted throughout the main feed every 3 posts.

  • show the code that displays your posts on the homepage – RiddleMeThis Dec 17 '18 at 16:56
  • Sure thing, I've gone ahead and added the code with additional explanation, hope that helps. Thanks! – user5854648 Dec 17 '18 at 17:04
  • Related: It's not a good idea to use query_posts(). – Pat J Dec 17 '18 at 19:20
  • ...Though on second reading, I note you're using wp_reset_query(), which is good practice if you insist on using query_posts(). – Pat J Dec 17 '18 at 19:28
0

I've found old pieces of code and joined it with yours. It could be more elegant, but you have the idea.

<?php
$args = array(
    'category__not_in' => array( 20 ),
);

$query = new WP_Query( $args );
$posts = $query->posts;
wp_reset_postdata();

$post_counter = 1;

// secondary posts' categories (ids)
$sec_categories = [2, 14, 56, 234];
$cat_counter = 0;


foreach( $posts as $post ) {
    echo '<h1>' . $post->post_title . '</h1>';
    echo '<p>' . $post->post_excerpt . '</p>';

    // after each 3rd post goes (two) secondary
    if( $post_counter % 3 == 0 ) {
        query_posts( 'cat=' . $sec_categories[$cat_counter] . '&posts_per_page=2' );
        while ( have_posts() ) {
            the_post();
            echo '<h2>' . get_the_title() . '</h2>';
            echo '<p>' . get_the_excerpt() . '</p>';
        }

        wp_reset_query();

        $cat_counter++;
    }

    $post_counter++;
}

You have to think out how to choose categories for secondary posts

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.