$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>query - My custom pagination not displaying|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

query - My custom pagination not displaying

matteradmin10PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far