0

i am using kriesi Pagination, it is working perfectly fine category page but it when i do similar query on page then it is not working. any idea why ?

function kriesi_pagination($pages = '', $range = 2){  
 $showitems = ($range * 2)+1;  
 global $paged;
 if(empty($paged)) $paged = 1;
 if($pages == '')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }   

 if(1 != $pages)
 {
     echo "<div class='pagination'>";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
     if($paged > 1) echo "<a class='prev' href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
         }
     }

     if ($paged < $pages) echo "<a class='next' href='".get_pagenum_link($paged + 1)."'>Next &rsaquo;</a>";  
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
     echo "</div>\n";
 }

}

my page query is

    <?php query_posts(array('category_name' => 'm-directory',
                          'meta_key'=> 'mno',
                         'orderby'=> 'meta_value',
                         'order'=> 'ASC')); if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>       <li class="post">  
     ...
   </li>  <?php endwhile;  kriesi_pagination(); ?>     <?php else : ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php

endif; ?>

  • Did my initial answer help? How about the revised one? Please be responsive. – Sally CJ Jun 3 at 14:23
1
+50

it is working perfectly fine on category page but when I do similar query on page, then it is not working

I can see why you used query_posts() — your pagination function (kriesi_pagination()) uses the main query (the global $wp_query variable) to get the number of pages for your query, so you thought that altering the main query using query_posts() would make the pagination work, right?

And basically, it would. But your query args is missing the paged parameter — more details here.

Nonetheless, there's no need to use query_posts() because your custom pagination function actually allows passing a custom number of pages which is the first parameter for the function.

Therefore, just make a new instance of WP_Query and pass the value of the instance's $max_num_pages property (i.e. WP_Query::$max_num_pages) to the pagination function:

$query = new WP_Query( [
    // .. your args here.
    'paged' => max( 1, get_query_var( 'paged' ) ),
] );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) :
        $query->the_post();

        // .. your code.
    endwhile;
endif;

kriesi_pagination( $query->max_num_pages );

wp_reset_postdata();

Additional Notes

  • Every time you make a custom/secondary instance of WP_Query and calls the_post() (or setup_postdata()), make sure to call wp_reset_postdata() after your loop ends, so that the global $post variable is restored back to the current post in the main query. Otherwise, for example, the next call to the_title() to display the title of the current post in the main query, you'd see the title of the last post in the above query and not the supposedly main query.

  • You should also know that on category/archive/search/etc. "plural" pages, the $wp_query->max_num_pages value could be 2 or more depending on the total number of posts in the main query and the posts per page setting (which defaults to 10). So if by "my page", you mean a Page (i.e. a post of the page type) which is a singular post, then the $wp_query->max_num_pages would always be 1. And that might be the answer to "any idea why ?" in the question.

  • If kriesi_pagination() still doesn't work for you, you can try using paginate_links():

    echo paginate_links( [
        'total'   => $query->max_num_pages,
        'current' => max( 1, get_query_var( 'paged' ) ),
    ] );
    

And last but not least, avoid using query_posts() as the WordPress core team suggested:

Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the pre_get_posts action within WP_Query.

Happy coding!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.