我在尝试为我的自定义帖子类型和自定义分类法显示数字分页时,在我自己的几个插件中遇到过这种情况。
我的理解是WP_Query 以自己的自定义方式(默认方式除外)从数据库中获取帖子将阻止您按预期使用此类函数。
它们用于默认的posts查询。因此,如果您希望按预期使用这些功能,则需要在执行查询之前修改默认查询。你可以用钩子pre_get_posts.
但是,如果要使用自己的自定义查询,请使用WP_Query, 然后还需要自定义数字分页的工作方式。使用标准WordPress功能可以获得相同的最终结果,但它需要额外几行代码才能实现预期的数字分页。
Let\'s say you have a custom query using WP_Query and it looks something like this: 
$query = new WP_Query( $query_args ); 
这意味着您有一个全新的查询,它与加载页面时执行的主默认查询不同。
Now WordPress is aware of TWO queries for the current page:
- Query #1 作为默认查询
- Query #2 作为您刚才使用创建的自定义查询WP_Query.
如果希望数字分页与自定义查询配合使用,则需要了解执行请求的当前页面。
The current page number is actually referred to as paged. You can determine the current page number using some code like this:
$current_page = 1;
if ( get_query_var( \'paged\' ) ) {
    $current_page = get_query_var( \'paged\' );
}
Once the current page has been determined, you can setup your custom query to look similar to this:
$query_args = array(
    \'post_type\'      => \'products\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' => 10,
    \'paged\'          => absint( $current_page )
);
$query = new WP_Query( $query_args );
Once the query has been performed, you then need to check if it\'s located any posts in the database which match your criteria:
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Post content here...
    }
} else {
    // No posts...
}
 现在开始数字分页!我建议使用一个名为
paginate_links() 对于此部分。
我真的不太明白base, 但这就是上面链接的WordPress Codex页面所展示的内容。
您需要指定当前页面(paged) -- 再一次你需要知道帖子的总数(可以从$query 变量对象)需要为此函数指定返回类型。(就我个人而言,我希望它是数组格式的,这样我就可以根据自己的喜好定制分页)
$big = 999999999; // need an unlikely integer
$previous_text = \'« Previous\';
$next_text     = \'Next »\';
$pagination = paginate_links( array(
    \'base\'      => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\'    => \'?current_page=%#%\',
    \'current\'   => max( 1, $current_page ),
    \'total\'     => $query->max_num_pages,
    \'type\'      => \'array\',
    \'prev_text\' => $previous_text,
    \'next_text\' => $next_text
) );
 此代码需要置于声明我们有可用帖子的条件内。不是在循环内部,而是在循环之后。
一旦配置了数字分页,就可以输出(或显示)。这是分页项目返回的位置array 总体安排
我喜欢用条件检查来包装它,然后在执行附加条件检查的项目中循环。
if ( $pagination ) {
    $content .= \'<div class="clearfix"></div>\' . PHP_EOL;
    $content .= \'<nav id="numerical-pagination" class="text-center">\' . PHP_EOL;
    $content .= \'<ul class="pagination">\' . PHP_EOL;
    foreach ( $pagination as $pagination_item ) {
        $class = \'\';
        if ( stripos( $pagination_item, \'current\' ) !== false ) {
            $class = \' class="active"\';
        }
        if ( stripos( $pagination_item, \'dots\' ) !== false ) {
            $class = \' class="disabled"\';
        }
        $content .= \'<li\' . $class . \'>\' . $pagination_item . \'</li>\' . PHP_EOL;
    }
    $content .= \'</ul>\' . PHP_EOL;
    $content .= \'</nav><!-- end #numerical-pagination .text-center -->\' . PHP_EOL;
    $content .= \'<div class="clearfix"></div>\' . PHP_EOL;
    echo $content;
}
Note: 我对WordPress主题使用Bootstrap。所以,如果你想让分页看起来很棒,只需要很少的努力。测试
Bootstrap.
综上所述,这里有一个多功能复制/粘贴入门解决方案,任何人都可以使用WP_Query.
$current_page = 1;
if ( get_query_var( \'paged\' ) ) {
    $current_page = get_query_var( \'paged\' );
}
$query_args = array(
    \'post_type\'      => \'products\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' => 10,
    \'paged\'          => absint( $current_page )
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Post content here...
    }
    $big = 999999999; // need an unlikely integer
    $previous_text = \'« Previous\';
    $next_text     = \'Next »\';
    $pagination = paginate_links( array(
        \'base\'      => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\'    => \'?current_page=%#%\',
        \'current\'   => max( 1, $current_page ),
        \'total\'     => $query->max_num_pages,
        \'type\'      => \'array\',
        \'prev_text\' => $previous_text,
        \'next_text\' => $next_text
    ) );
    $content = \'\';
    if ( $pagination ) {
        $content .= \'<div class="clearfix"></div>\' . PHP_EOL;
        $content .= \'<nav id="numerical-pagination" class="text-center">\' . PHP_EOL;
        $content .= \'<ul class="pagination">\' . PHP_EOL;
        foreach ( $pagination as $pagination_item ) {
            $class = \'\';
            if ( stripos( $pagination_item, \'current\' ) !== false ) {
                $class = \' class="active"\';
            }
            if ( stripos( $pagination_item, \'dots\' ) !== false ) {
                $class = \' class="disabled"\';
            }
            $content .= \'<li\' . $class . \'>\' . $pagination_item . \'</li>\' . PHP_EOL;
        }
        $content .= \'</ul>\' . PHP_EOL;
        $content .= \'</nav><!-- end #numerical-pagination .text-center -->\' . PHP_EOL;
        $content .= \'<div class="clearfix"></div>\' . PHP_EOL;
        echo $content;
    }
    wp_reset_postdata();
} else {
    // No posts...
}
wp_reset_query();