WordPress.org

Forums

[resolved] Pagination with custom loop not working (4 posts)

  1. macgamer
    Member
    Posted 2 years ago #

    On my site, I have a front page template with a custom loop query:

    <?php $args = array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => 10, 'cat' => '-7, -8, -112','paged' =>get_query_var('paged') ); ?>
    
    				<?php $loop = new WP_Query( $args ); ?>
    
    				<?php if ( $loop->have_posts() ) : ?>
    
    					<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    Then after the loop,

    <?php get_template_part( 'loop-nav' ); ?>

    (Which is used throughout the rest of the site.)
    However, the pagination never shows, and setting the url to /page/2 doesn't change which posts get displayed. I've also tried just replacing the loop-nav template with stock WP pagination code, but that doesn't work either.

    Can anyone shed some light on this?

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    'loop-nav' is possibly/probably based on the default $wp_query object, while you are using $loop (?)

    what is the code of loop-nav.php?

    possibly try this hack:

    <?php $temp = $wp_query; $wp_query = null; $wp_query = $loop;
    get_template_part( 'loop-nav' );
    $wp_query = null; $wp_query = $temp; ?>

    slightly related:
    http://wordpress.stackexchange.com/questions/20424/wp-query-and-next-posts-link

  3. keesiemeijer
    moderator
    Posted 2 years ago #

    Also, if this is a in a static front page (Page template) you need to use the "page" query variable.

    <?php $args = array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => 10, 'cat' => '-7, -8, -112','paged' =>get_query_var('page') ); ?>

    http://codex.wordpress.org/Pagination#static_front_page

  4. macgamer
    Member
    Posted 2 years ago #

    @keesiemeijer: Thanks, your link solved it!

Topic Closed

This topic has been closed to new replies.

About this Topic