0

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,
         ) );
    }
}

}

0

You 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

  • Hello, your version of the function seems very good, but strangely I still have nothing that appears. And when I inspect the area of ​​the function call with my browser, notthing is displayed – Frenchy_WP Mar 6 at 14:55
  • Is it showing all posts? – Chinmoy Kumar Paul Mar 6 at 15:03
  • can you remove it 'numberposts' => -1, from your 1st snippet? – Chinmoy Kumar Paul Mar 6 at 15:05
  • I deleted "'postsposts' => -1" I still have no change. But I noticed that despite "posts_per_page" => 6 "all my posts have posted (8 in all) – Frenchy_WP Mar 6 at 15:23

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.