• Ok, this is the situation:

    I have looking this from 2-3 days on web but still unsuccessful. I found many snippets but none matching my requirement. Currently, my main loop shows a particular category post list.
    I want to add a different category post at even post indexes.
    Ex:

    Current post list                     Required post list
    cat1                                     cat1
    cat1                                     cat2
    cat1                                     cat1
                                             cat2
                                             cat1

    Many times I have broken my while loop and been reading a lot I can’t find a solution, so any kind of help would be greatly appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Can you paste your code so that we can see what you are currently doing?

    Thread Starter busybe235

    (@busybe235)

    $k = round(get_option('posts_per_page')/2);
    
    <?php for($y=0;$k<20;$y++) { ?>
    
     <?php if ( have_posts() ) { ?>
     <?php if(is_home()) query_posts("posts_per_page=1&cat=1&offset=$y"); ?>
     <?php while ( have_posts() ) : the_post(); ?>
     <?php get_template_part( 'content', get_post_format() ); ?>
     <?php endwhile; }  ?>
    
     <?php if ( have_posts() ) { ?>
     <?php if(is_home()) query_posts("posts_per_page=1&cat=2&offset=$y"); ?>
     <?php while ( have_posts() ) : the_post(); ?>
     <?php get_template_part( 'content', get_post_format() ); ?>
     <?php endwhile; } } }?>

    This is what I’m trying. Using two while loops displaying single post of cat1 and cat2 and enclosing them in for loop to run. I couldn’t get it to work.

    Have you tried ending your if statements?

    Thread Starter busybe235

    (@busybe235)

    like how, I don’t get it!

    Moderator keesiemeijer

    (@keesiemeijer)

    Where do you want this? On your home page. Is this template used for other page requests as well (category, archives, etc..). It’s best you use one loop and just add a post from cat 2 after every post from cat 1.

    Here is an example on how you can do this:

    <?php
    // change these category IDs to your category IDs
    $cat_1_id = 1;
    $cat_2_id = 2;
    
    $posts_per_page = get_option( 'posts_per_page' );
    
    // set the paged variable
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    ?>
    
    <?php
    // the query for category 1
    $args = array(
    	'posts_per_page' => $posts_per_page,
    	'paged'          => $paged,
    	'cat'            => $cat_1_id,
    );
    
    $cat_1_query = new WP_Query( $args );
    ?>
    
    <?php
    // the query for category 2
    $args['cat'] = $cat_2_id;
    
    $cat_2_query = new WP_Query( $args );
    ?>
    
    <?
    // use the query with the most posts (for pagination)
    $the_query = ( $cat_1_query->found_posts >= $cat_2_query->found_posts ) ? $cat_1_query : $cat_2_query;
    ?>
    <!-- the loop -->
    <?php if ( $the_query->have_posts() ) : ?>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    		<?php
    		$current_post = $the_query->current_post;
    		global $post;
    		$post_temp = $post;
    		?>
    
    		<!-- post from cat 1 -->
    	   	<?php
    		if ( isset( $cat_1_query->posts[ $current_post ] ) ) :
    			$post = $cat_1_query->posts[ $current_post ];
    			setup_postdata( $post );
    		?>
    
    	   	<!-- normal loop stuff for post cat 1 post -->
    		<?php get_template_part( 'content', get_post_format() ); ?>
    
    		<?php endif; ?>
    
    		<!-- post from cat 2 -->
    		<?php
    		if ( isset( $cat_2_query->posts[ $current_post ] ) ) :
    			$post = $cat_2_query->posts[ $current_post ];
    			setup_postdata( $post );
    ?>
    
    	   	<!-- normal loop stuff for cat 2 post -->
    		<?php get_template_part( 'content', get_post_format() ); ?>
    
    		<?php endif; ?>
    
    		<?php
    		// reset_post to original $post object
    		$post = $post_temp;
    		?>
    
    	<?php endwhile; ?>
    
    	<!-- pagination functions here -->
    
    	<?php wp_reset_postdata(); ?>
    
    <?php endif; // end have_posts() check ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding specific category post at even positions of main loop’ is closed to new replies.