最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Insert Content In Between Post Feed

matteradmin9PV0评论

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.

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.

Share Improve this question edited Dec 17, 2018 at 17:04 user5854648 asked Dec 17, 2018 at 16:51 user5854648user5854648 16317 bronze badges 4
  • show the code that displays your posts on the homepage – RiddleMeThis Commented Dec 17, 2018 at 16:56
  • Sure thing, I've gone ahead and added the code with additional explanation, hope that helps. Thanks! – user5854648 Commented Dec 17, 2018 at 17:04
  • Related: It's not a good idea to use query_posts(). – Pat J Commented Dec 17, 2018 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 Commented Dec 17, 2018 at 19:28
Add a comment  | 

1 Answer 1

Reset to default 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far