I'm testing a custom pagination feature but my pagination is not showing, is there something I forgot to do?
here's my arguments and query inserted in my template:
<?php
global $wp_query, $wpex_query;
if ( $wpex_query ) {
$total = $wpex_query->max_num_pages;
} else {
$total = $wp_query->max_num_pages;
}
$args = array(
'post_type' => 'post',
'category_name' => 'templates-wordpress',
'post_status'=>'publish',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null,
'posts_per_page' => 6,
'paged' => $paged //very important
);
$wpex_query = new WP_Query( $args );
if ($wpex_query->have_posts()) :
?>
And here is my function:
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
I'm testing a custom pagination feature but my pagination is not showing, is there something I forgot to do?
here's my arguments and query inserted in my template:
<?php
global $wp_query, $wpex_query;
if ( $wpex_query ) {
$total = $wpex_query->max_num_pages;
} else {
$total = $wp_query->max_num_pages;
}
$args = array(
'post_type' => 'post',
'category_name' => 'templates-wordpress',
'post_status'=>'publish',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null,
'posts_per_page' => 6,
'paged' => $paged //very important
);
$wpex_query = new WP_Query( $args );
if ($wpex_query->have_posts()) :
?>
And here is my function:
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
Share Improve this question asked Mar 6, 2019 at 13:36 Frenchy_WPFrenchy_WP 191 silver badge6 bronze badges1 Answer
Reset to default 0You will pass the $wpex_query object into wpex_pagination function. Currently $wp_query object is not getting your custom query details. Here is the updated for wpex_pagination function.
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination( $new_query ) {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$temp_query = $wp_query; // saving old $wp_query object into temp variable
$wp_query = $new_query; // updating old $wp_query object with $new_query object
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
$wp_query = $temp_query; // revert back to old $wp_query object
$temp_query = '';
}
}
Now you will call the wpex_pagination function like this way wpex_pagination( $wpex_query )
in your php file