php - Wordpress Pagination with custom query loads same content on every page -
php - Wordpress Pagination with custom query loads same content on every page -
i using 2 custom queries load posts on custom wordpress template homepage. first query loads 4 'sticky' posts, , next query loads several more recent posts.
this works great , need do, pagination links when clicked show same content on every page.
for illustration have 10 posts on homepage, , when click page 2 - exact same 10 posts appear again. if click pagination visit page 3, same 10 posts 1 time again etc. no matter page on, same 10 posts listed.
i have read paged beingness required several failed attempts include in below code have broguht me here.
any ideas appreciated!
my code...
<?php $sticky = get_option( 'sticky_posts' ); $args = array( 'posts_per_page' => 4, 'post__in' => $sticky, 'ignore_sticky_posts' => 1, ); ?> <?php $the_query = new wp_query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <a href="<?php the_permalink() ?>"<?php the_title(); ?>"></a> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'sorry, no posts matched criteria.' ); ?></p> <?php endif; ?> <main id="main" class="site-main" role="main"> <?php $the_query = new wp_query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php else : ?> <?php get_template_part( 'content', 'none' ); ?> <?php endif; ?>
you sure need $paged
parameter (docs). untested give go:
<?php // $paged holds current page offset $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1; /** * - checks if current page 'paged' (false on first page) * - remove check if need sticky posts on pages * - added $paged parameter sticky posts paginate * if decide show them on pages * */ if( ! is_paged() ) { // sticky posts on first page $sticky = get_option( 'sticky_posts' ); $args = array( 'posts_per_page' => 4, 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'paged' => $paged // ah, page offset ); $the_query = new wp_query( $args ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); ?> <a href="<?php the_permalink() ?>"><?php the_title(); ?>"></a> <?php } } else { ?> <p><?php _e( 'sorry, no posts matched criteria.' ); ?></p> <?php } wp_reset_postdata(); } ?> <main id="main" class="site-main" role="main"> <?php $args = array( 'post__not_in' => get_option( 'sticky_posts' ), 'paged' => $paged // ah, page offset ); // can done via new wp_query today lazy // see http://codex.wordpress.org/function_reference/query_posts query_posts($args); if ( have_posts() ) { while ( have_posts() ) { the_post(); get_template_part( 'content', get_post_format() ); } } else { get_template_part( 'content', 'none' ); } wp_reset_query(); ?>
php loops pagination wordpress
Comments
Post a Comment