• Resolved Blue Phoenix

    (@blue-phoenix)


    I’m trying to get pagination working on a test site that I’m developing with the hope to add Ajax to use a load more function however at present my pages display the same 5 posts every time I click on the next page link. Would appreciate some advice on this

    <?php
                $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
                $args=array(
                'caller_get_posts'=>1,
                'offset' => 1,
                'orderby' => 'title',
                'order'   => 'DESC',
                'posts_per_page' => 5,
                'paged' => $page
            );
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() )  {
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <article class="row sub-article">
                        <a href="<?php the_permalink();?>" title="<?php the_title();?>">
                            <div class="col-xs-4">
                                <?php the_post_thumbnail('secondary-thumb', array('class' => 'img-responsive')); ?>
                            </div>
                            <div class="col-xs-8">
                                <h4><?php the_title();?></h4>
                            </div>
                        </a>
                    </article>
    
                <?php endwhile;} ?>
                <?php posts_nav_link(); ?>
                <?php wp_reset_postdata(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • try and remove the 'offset' parameter, and try to use a different way to exclude the first post.

    also, posts_nav_link() only works on the default query;
    try to work with https://codex.wordpress.org/Function_Reference/next_posts_link (particular review https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query ) and https://codex.wordpress.org/Function_Reference/previous_posts_link (apply the same coding as for ‘next_posts_link()’)

    Thread Starter Blue Phoenix

    (@blue-phoenix)

    Thanks for the steer, really helped and managed to find a working solution

    <?php
                $display_count = 5;
                $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
                $offset = ($display_count * ($page - 1)) + 1;
    
                $args=array(
                'caller_get_posts'=>1,
                'orderby' => 'title',
                'order'   => 'DESC',
                'posts_per_page' => $display_count,
                'page'       =>  $page,
    	        'offset'     =>  $offset
            );
                $my_query = new WP_Query($args);
                if( $my_query->have_posts() )  {
                while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <article class="row sub-article">
                        <a href="<?php the_permalink();?>" title="<?php the_title();?>">
                            <div class="col-xs-4">
                                <?php the_post_thumbnail('secondary-thumb', array('class' => 'img-responsive')); ?>
                            </div>
                            <div class="col-xs-8">
                                <h4><?php the_title();?></h4>
                            </div>
                        </a>
                    </article>
    
                <?php endwhile;} ?>
                <?php next_posts_link(); ?>
                <?php previous_posts_link(); ?>
                <?php wp_reset_postdata(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Pagination Displaying Same Content’ is closed to new replies.