简体   繁体   English

Wordpress 分页 - 帖子数量过多

[英]Wordpress Pagination - Too many pages for number of posts

I've added pagination for a custom post type within my Wordpress theme.我在我的 Wordpress 主题中为自定义帖子类型添加了分页。 It works great, apart from it shows too many pages in the pagination menu for the number of posts: http://www.electrickiwi.co.uk/testimonials/除了它在分页菜单中为帖子数量显示太多页面之外,它效果很好: http : //www.electrickiwi.co.uk/testimonials/

There should currently be 7 pages, but it's showing 12. The code below is what I am using to display the pagination.当前应该有 7 页,但显示 12 页。下面的代码是我用来显示分页的代码。 There is nothing in the functions.php file relating to this.在functions.php 文件中没有与此相关的任何内容。

<?php
/* ------------------------------------------------------------------*/
/* PAGINATION */
/* ------------------------------------------------------------------*/

//paste this where the pagination must appear

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ($total > 1) {
    // get the current page
    if (!$current_page = get_query_var('paged')) {
        $current_page = 1;
    }
    // structure of "format" depends on whether we're using pretty permalinks
    if (get_option('permalink_structure')) {
        $format = 'page/%#%/';
    }
    else {
        $format = 'page/%#%/';
    }
    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => $format,
        'current' => $current_page,
        'total' => $total,
        'mid_size' => 4,
        'type' => 'list'
    ));
}
?>

从传递给 paginate_links 的数组中删除'total' => $total部分,以便自动计算页数。

我通过将 ' total' => $total更改为 ' total' => $dataQuery->max_num_pages,

I was having this exact same issue, but was able to solve it by using 'total' => $my_query->max_num_pages, like this:我遇到了完全相同的问题,但能够通过使用 'total' => $my_query->max_num_pages 来解决它,如下所示:

$args[ 'post_type' ] = array('resources', 'case_study');
$args['post_status'] = 'publish';

$my_query = new WP_Query( $args );
echo the_posts_pagination( array(
    'mid_size' => 3,
    'total' => $my_query->max_num_pages,
) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM