我的自定义分页不显示

时间:2019-03-06 作者:Frenchy_WP

我正在测试一个自定义分页功能,但我的分页没有显示,有什么我忘记了做的吗?

以下是插入到模板中的参数和查询:

<?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()) :
                    ?> 
这是我的功能:

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

1 个回复
SO网友:Chinmoy Kumar Paul

您将通过$wpex_query 对象到wpex_pagination 作用目前$wp_query 对象未获取您的自定义查询详细信息。以下是更新的wpex\\u分页函数。

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 = \'\';
    }
}
现在您将调用wpex_pagination 像这样工作wpex_pagination( $wpex_query ) 在php文件中

相关推荐

将本地jQuery替换为外部托管时出现WordPress错误

我试图从CDN加载jquery,而不是用wordpress本机加载。在我的功能中。php我已经完成了如下操作,确保它只发生在前端:function replace_jquery() { if (!is_admin()) { wp_deregister_script(\'jquery\'); wp_register_script(\'jquery2\', \'https://ajax.googleapis.com/ajax/libs/jquer