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)."'>«</a>";
if($paged > 1) echo "<a class='prev' href='".get_pagenum_link($paged - 1)."'>‹ 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 ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</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; ?>
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)."'>«</a>";
if($paged > 1) echo "<a class='prev' href='".get_pagenum_link($paged - 1)."'>‹ 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 ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
my page query is
Share Improve this question asked May 28, 2020 at 14:12 pagolpagol 2032 gold badges4 silver badges18 bronze badges 1<?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 Commented Jun 3, 2020 at 14:23
1 Answer
Reset to default 1 +50it 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 callsthe_post()
(orsetup_postdata()
), make sure to callwp_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 tothe_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 to10
). So if by "my page", you mean a Page (i.e. a post of thepage
type) which is a singular post, then the$wp_query->max_num_pages
would always be1
. 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 usingpaginate_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 withinWP_Query
.
Happy coding!