• Hi everybody,
    are you ready for my strange help request?
    ok, let’s start.
    I need to paginate my multiple loops shown on the home: the first loop contains 2 posts from one category (some sort of featured), the second loop 8 post from all the categories, but obv i don’t whant the same article is shown twice in both loops.
    I need the first loop to disappear from the non-first pages, so both in the home and only the second loop from the second page to the end.
    This are the loops now:

    <?php
    // Let's fix pagination issue first
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    if(1 == $paged) {
    }
    
    $args=array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'paged' => $paged,
    	'post___in' => 'cat=32',
    	'posts_per_page' => 2
    );
    $wp_query = new WP_Query('cat=32');
    $wp_query->query($args);
    ?>
    <?php if ( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    	//First loop template here
    
    	<?php endwhile; ?>
    
    <?php
    // Let's fix pagination issue first
    
    if ( get_query_var('paged') ) {
    	$paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    	$paged = get_query_var('page');
    } else {
    	$paged = 1;
    }
    
    $args=array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'paged' => $paged,
    	'post__not_in' => $do_not_duplicate,
    	'posts_per_page' => 8
    );
    $wp_query = new WP_Query();
    $wp_query->query($args);
    ?>
    <?php if ( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    // Secon loop template here
    <?php endwhile; ?>
    	<?php wp_reset_postdata(); ?>
    <?php endif; ?>
    
    	<?php if(function_exists('wp_pagenavi')): wp_pagenavi(); ?>
    	<?php else: ?>
    		<?php global $wp_query; if($wp_query->max_num_pages > 1) { ?>
    		<div class="navigation clearfix">
    			<span class="prev"><?php previous_posts_link( __('&larr; Previous', 'warrior') ); ?></span>
    			<span class="next"><?php next_posts_link( __('Next &rarr;', 'warrior') ); ?></span>
    		</div>
    		<?php } ?>
    	<?php endif; ?>
    		<?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not quite sure what you’re asking. In part it sounds like you want to know how to eliminate the first 2 posts from the second query. But you seem to have done that with $do_not_duplicate though the code for how this array is built is missing. I expect it’s there in your original code, so I’ll assume that part is covered.

    So I suppose your question is about pagination. You are trying to use pagination from the main query to work for your sub-queries. This never seems to work well. I’m not saying it can’t be done, but it does cause people a lot of trouble. IMO, you are better off either managing pagination yourself (including the next and previous links); or, if you are not using the main query, convert it to your needs through ‘pre_get_posts’ and make the first two posts sticky posts. You can change which posts are sticky posts by code, so this approach should work for you as long as you do not need the main query for something.

Viewing 1 replies (of 1 total)

The topic ‘Multiple loops and pagination’ is closed to new replies.