简体   繁体   中英

Wordpress Pagination with custom query loads same content on every page

I am using 2 custom queries to load posts on a custom Wordpress template homepage. the first query loads 4 'sticky' posts, and the next query loads several more recent posts.

This works great and does what I need it to do, but the pagination links when clicked show the same content on every page.

For example I have 10 posts on the homepage, and when i click to page 2 - the exact same 10 posts appear again. If I click pagination to visit page 3, the same 10 posts again etc. No matter what page I am on, the same 10 posts are listed.

I have read something about paged being required but several failed attempts to include that in my 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 your 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 the $paged parameter ( docs ). Untested but give it a go:

<?php
// $paged holds the current page offset
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;

/**
 * - Checks if the current page is 'paged' (false on first page)
 * - Remove the check if you need those sticky posts on all pages
 * - I added the $paged parameter so those sticky posts will paginate 
 *   if you decide to show them on all pages
 * 
 */
if( ! is_paged() ) { // Sticky posts only on first page
    $sticky = get_option( 'sticky_posts' );
    $args = array(
        'posts_per_page' => 4,
        'post__in'  => $sticky,
        'ignore_sticky_posts' => 1,
        'paged' => $paged // ah, the 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 your 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, the page offset
    );
    // can be done via new WP_Query but today I am lazy
    // also 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();
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM