充分利用分页功能来处理定制查询

时间:2020-04-03 作者:RLM

我正在使用understrap Wordpress boiler plate theme. 它有一个分页功能,如果您将其放入以下标准循环中,该功能将正常工作:

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <?php endwhile; ?>

    <?php else : ?>
    <?php endif; ?>
    <?php understrap_pagination(); ?>

<?php understrap_pagination(); ?>
实际代码如下:

 if ( ! function_exists( \'understrap_pagination\' ) ) {
        /**
         * Displays the navigation to next/previous set of posts.
         *
         * @param string|array $args {
         *     (Optional) Array of arguments for generating paginated links for archives.
         *
         *     @type string $base               Base of the paginated url. Default empty.
         *     @type string $format             Format for the pagination structure. Default empty.
         *     @type int    $total              The total amount of pages. Default is the value WP_Query\'s
         *                                      `max_num_pages` or 1.
         *     @type int    $current            The current page number. Default is \'paged\' query var or 1.
         *     @type string $aria_current       The value for the aria-current attribute. Possible values are \'page\',
         *                                      \'step\', \'location\', \'date\', \'time\', \'true\', \'false\'. Default is \'page\'.
         *     @type bool   $show_all           Whether to show all pages. Default false.
         *     @type int    $end_size           How many numbers on either the start and the end list edges.
         *                                      Default 1.
         *     @type int    $mid_size           How many numbers to either side of the current pages. Default 2.
         *     @type bool   $prev_next          Whether to include the previous and next links in the list. Default true.
         *     @type bool   $prev_text          The previous page text. Default \'&laquo;\'.
         *     @type bool   $next_text          The next page text. Default \'&raquo;\'.
         *     @type string $type               Controls format of the returned value. Possible values are \'plain\',
         *                                      \'array\' and \'list\'. Default is \'array\'.
         *     @type array  $add_args           An array of query args to add. Default false.
         *     @type string $add_fragment       A string to append to each link. Default empty.
         *     @type string $before_page_number A string to appear before the page number. Default empty.
         *     @type string $after_page_number  A string to append after the page number. Default empty.
         *     @type string $screen_reader_text Screen reader text for the nav element. Default \'Posts navigation\'.
         * }
         * @param string       $class           (Optional) Classes to be added to the <ul> element. Default \'pagination\'.
         */
        function understrap_pagination( $args = array(), $class = \'pagination\' ) {

            if ( ! isset( $args[\'total\'] ) && $GLOBALS[\'wp_query\']->max_num_pages <= 1 ) {
                return;
            }

            $args = wp_parse_args(
                $args,
                array(
                    \'mid_size\'           => 2,
                    \'prev_next\'          => true,
                    \'prev_text\'          => __( \'&laquo;\', \'understrap\' ),
                    \'next_text\'          => __( \'&raquo;\', \'understrap\' ),
                    \'type\'               => \'array\',
                    \'current\'            => max( 1, get_query_var( \'paged\' ) ),
                    \'screen_reader_text\' => __( \'Posts navigation\', \'understrap\' ),
                )
            );

            $links = paginate_links( $args );
            if ( ! $links ) {
                return;
            }

            ?>

            <nav aria-labelledby="posts-nav-label">

                <h2 id="posts-nav-label" class="sr-only">
                    <?php echo esc_html( $args[\'screen_reader_text\'] ); ?>
                </h2>

                <ul class="<?php echo esc_attr( $class ); ?>">

                    <?php
                    foreach ( $links as $key => $link ) {
                        ?>
                        <li class="page-item <?php echo strpos( $link, \'current\' ) ? \'active\' : \'\'; ?>">
                            <?php echo str_replace( \'page-numbers\', \'page-link\', $link ); ?>
                        </li>
                        <?php
                    }
                    ?>

                </ul>

            </nav>

            <?php
        }
    }
但我遇到的问题是分页函数对如下自定义循环没有任何作用:

 <?php
      $catquery = new WP_Query( array(
      \'orderby\' => date,
      \'order\' => \'DESC\',
      \'cat\' => \'6\'
    ));
    ?>

    <?php if ( have_posts() ) : ?>
      <?php while($catquery->have_posts()) : $catquery->the_post(); ?>
       <?php endwhile;
      wp_reset_postdata();
      ?>
如何修改它以在该类型的循环中工作?如果我遗漏了什么,请告诉我,以帮助您更好地回答问题。提前谢谢。

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

分页函数不处理自定义循环

这是因为对于自定义循环或次循环WP_Query 实例,如$catquery 变量,则需要显式告诉函数有多少页可用于自定义查询。

所以$total 参数(请参见函数的说明)应设置为max_num_pages property 查询对象,即$catquery 变量:

understrap_pagination( [
    \'total\' => $catquery->max_num_pages,
] );
就这么简单,上面的方法应该可以奏效。:)