i hope you help me with this,
i am making a wordpress website witch has 3 diffrent post types(the regular post type and 2 others), i have the pagenation work very with in the regular posts page, but it does not work in the other 2 page of the other custom posts, when i tried to solve this out i found that the problem is that like the custom query object does not have the max_num_pages
attribute, when i echo the value of it, it does not show of ,
look at my code!
this is the custom query i made:
$currentpage = get_query_var('paged');
$myQuery= new WP_Query(array(
'post_type' => 'airdrop',
'posts_per_page' => 3,
'paged' => $currentpage
))
function my_pagination(){
$all_pages = $myQuery->max_num_pages;
$current_page = max(1 , get_query_var('paged'));
echo $current_page ."/". $all_pages;
if ($all_pages>1) {
return paginate_links(array(
'base' =>get_pagenum_link() .'%_%',
'format' =>'page/%#%',
'current' =>$current_page
));
}
}
echo my_pagination();
if you look at this line echo $current_page ."/". $all_pages;
this should print something like 1 / 2 or 1 /3
,
but it shows only 1 / blank
and the num_max not shwing,
i hope you help me figure it out
EDIT :
even this simple codeisn't working
$loop = new WP_Query(array(
'post_type' => 'airdrop',
'posts_per_page' => 3,
));
echo $loop->max_num_comment_pages;
i hope you help me with this,
i am making a wordpress website witch has 3 diffrent post types(the regular post type and 2 others), i have the pagenation work very with in the regular posts page, but it does not work in the other 2 page of the other custom posts, when i tried to solve this out i found that the problem is that like the custom query object does not have the max_num_pages
attribute, when i echo the value of it, it does not show of ,
look at my code!
this is the custom query i made:
$currentpage = get_query_var('paged');
$myQuery= new WP_Query(array(
'post_type' => 'airdrop',
'posts_per_page' => 3,
'paged' => $currentpage
))
function my_pagination(){
$all_pages = $myQuery->max_num_pages;
$current_page = max(1 , get_query_var('paged'));
echo $current_page ."/". $all_pages;
if ($all_pages>1) {
return paginate_links(array(
'base' =>get_pagenum_link() .'%_%',
'format' =>'page/%#%',
'current' =>$current_page
));
}
}
echo my_pagination();
if you look at this line echo $current_page ."/". $all_pages;
this should print something like 1 / 2 or 1 /3
,
but it shows only 1 / blank
and the num_max not shwing,
i hope you help me figure it out
EDIT :
even this simple codeisn't working
$loop = new WP_Query(array(
'post_type' => 'airdrop',
'posts_per_page' => 3,
));
echo $loop->max_num_comment_pages;
Your variables are not in functions scope.
My example of function:
function customPagination($paged = '', $max_page = '') {
global $wp_query;
$big = 999999999;
if( ! $paged )
$paged = get_query_var('paged');
if( ! $max_page )
$max_page = $wp_query->max_num_pages;
echo paginate_links( array(
'total' => $max_page,
'prev_next' => false,
'prev_text' => false,
'next_text' => false,
'total' => $wp_query->max_num_pages,
'mid_size' => 3
) );
}
and then calling it with your variables (just changed to camelCase)
customPagination($currentPage, $myQuery->max_num_pages);
Hope it helps
I found the answer : what i was doing wrong is that i have two wp_query objects with the same name ($loop).
when first i made the two posts type i gave theme same name, this is why it gets confused,
the solution is that never use two or more wp_query with the same name!!!
$myQuery
is defined outside and isn't accessible from withinmy_pagination()
. – Sally CJ Commented Oct 12, 2019 at 22:20WP_Query
objects/instances like this - and in your case, you could simply change your function tofunction my_pagination( $myQuery )
and then when you call the function, supply theWP_Query
instance like so:my_pagination( $myQuery )
. And looking at your current answer, that's an example problem of using global variables - when the same variable is assigned to anotherWP_Query
instance, then you'd run into issues. – Sally CJ Commented Oct 14, 2019 at 10:01