【问题标题】:Wordpress Pagination - Too many pages for number of postsWordpress 分页 - 帖子数量过多的页面
【发布时间】:2021-07-22 05:49:57
【问题描述】:

我在我的 Wordpress 主题中为自定义帖子类型添加了分页。它工作得很好,除了它在分页菜单中显示的帖子数量过多:http://www.electrickiwi.co.uk/testimonials/

目前应该有 7 页,但显示的是 12 页。下面的代码是我用来显示分页的代码。 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'
    ));
}
?>

【问题讨论】:

    标签: php wordpress pagination wordpress-theming


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答。当我删除它时,它只是从页面中完全删除了分页。
    【解决方案2】:

    我通过将 'total' =&gt; $total 更改为 'total' =&gt; $dataQuery-&gt;max_num_pages, 来解决此问题

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但能够通过使用 '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,
      ) );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多