admin管理员组

文章数量:1129123

Here is my loop

        $my_query = new WP_Query(array(
                'cat' => -399,
                'posts_per_page' => 6,
                'offset' => 5,
                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
        ));
        if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); 
            $imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $finalurl = get_stylesheet_directory_uri(). "/functions/thumb.php?w=180&h=180&a=t&src=".$imgurl;
            $date = get_the_date(); 
            $id = $post->ID;
        ?>
        .... loop contents ....

        <?php $finalurl = "";
        endwhile;
        else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <?php endif; wp_reset_query();
        $id = ""; wp_pagenavi();

Pagination shows same content in all pages. I need your suggestion.

Here is my loop

        $my_query = new WP_Query(array(
                'cat' => -399,
                'posts_per_page' => 6,
                'offset' => 5,
                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
        ));
        if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); 
            $imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $finalurl = get_stylesheet_directory_uri(). "/functions/thumb.php?w=180&h=180&a=t&src=".$imgurl;
            $date = get_the_date(); 
            $id = $post->ID;
        ?>
        .... loop contents ....

        <?php $finalurl = "";
        endwhile;
        else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <?php endif; wp_reset_query();
        $id = ""; wp_pagenavi();

Pagination shows same content in all pages. I need your suggestion.

Share Improve this question asked Feb 7, 2014 at 5:32 IFightCodeIFightCode 8032 gold badges12 silver badges26 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 8

Try changing $my_query to $wp_query to see if that fixes the issue. I found that when you rename the query it messes with the pagination.

Also you should move the reset query after the pagination. Here's a loop I've verified works with pagination:

$args = array(
    'posts_per_page' => 10,
    'post_type'      => 'post',
    'paged'          => get_query_var( 'paged' ),
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    get_template_part( 'templates/content', 'posts' );
endwhile;

/*
  PAGINATION
*/
if ( function_exists( 'page_navi' ) ) {
    ?>

    <div id="pagination">
        <?php page_navi(); ?>
    </div>
<?php }
wp_reset_query(); ?>

if you use wp_pagenavi in front page, yes, it won't work. you need to change the current paged in wp_pagenavi function like this:

// I changed the code below, you can see what different from yours.

if(!empty($paged)) {
    $paged = $paged;
}elseif(get_query_var( 'paged')) {
    $paged = get_query_var('paged');
}elseif(get_query_var( 'page')) {
    $paged = get_query_var('page');
}else {
    $paged = 1;
}

Here is an improvement on the answer by @Devin-Walker. This is specifically for archive.php but can be adapted for other loops.

Also it uses the native navigational links of WordPress instead of page_navi()

Hope this helps someone.

<?php
/**
 * Archive Page
 *
 * @Since   1.0
 */

// Exit if accessed directly
if ( !defined('ABSPATH')) exit;
?>

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <?php 

        // Note - Set number of posts per page in WP-Admin > Settings > Reading > 'Blog pages show at most'
        $args = array(
            'post_type'                 => 'post',
            'paged'                     => get_query_var('paged'),
            'cat'                       => get_query_var('cat'),
        );

        $wp_query = new WP_Query( $args );

        while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            <div class="col-md-4 post-card archive my-4">

                <div class="text-center">
                    <a href="<?php the_permalink(); ?>"><img src="<?php echo the_post_thumbnail_url('card-size'); ?>"></a>
                </div>
                <div class="post-card-details archive-post-details px-4 pb-2">
                    <h3 class="post-card-category px-4 py-2"><?php single_cat_title(); ?></h3>
                    <h3 class="post-card-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <p class="post-card-p"><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( get_post_field('post_content') ), 15 ) ); ?></p>
                </div>

            </div>

        <?php endwhile; ?>

    </div>

    <!-- Navigation -->
    <div class="tablet-pill tablet-pill-purple mb-5"><?php previous_posts_link( 'Previous Page' ); ?> <?php if(get_previous_posts_link() && get_next_posts_link()) echo ' - '; ?> <?php next_posts_link( 'Next Page' ); ?></div>

    <?php wp_reset_query(); ?>

</div>

<?php get_footer();

I know this was years ago, but I bring another solution for anyone experiencing a similar error, in my case I was using offset 0, so I removed the offset from the arguments of your query, which caused the problem, I hope it helps you. someone else:

Before

$query_args_pagination = array(
          'post_type' => $post_type_pagination,
          // 'posts_per_page' => $countPPP,
          'post_status'    => 'publish',
          'order'          => 'ASC',
          'orderby'        => 'date',
          'posts_per_page' => '12',
          'offset'         => '0',
          'paged' => $paged_pagination,
          // 'page' => $paged_pagination,
          // 'orderby' => 'date',
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy_pagination,
              'field' => 'slug',
              'terms' => $categoria_pagination,
            )
          )
        );

After

$query_args_pagination = array(
          'post_type' => $post_type_pagination,
          // 'posts_per_page' => $countPPP,
          'post_status'    => 'publish',
          'order'          => 'ASC',
          'orderby'        => 'date',
          'posts_per_page' => '12',
          // 'offset'         => '0',
          'paged' => $paged_pagination,
          // 'page' => $paged_pagination,
          // 'orderby' => 'date',
          'tax_query' => array(
            array(
              'taxonomy' => $taxonomy_pagination,
              'field' => 'slug',
              'terms' => $categoria_pagination,
            )
          )
        );

本文标签: Pagination shows same contents all pages