【问题标题】:Add transitional pages to pagination在分页中添加过渡页面
【发布时间】:2020-07-06 15:19:21
【问题描述】:

我现在需要为临时页面添加分页,当一个有 9 个页面时,它看起来像这样: 1 2 3 ... 9

我想给出一个介于 3 和 9 之间的中间点,即

1 2 3 ... 6 ... 9

当我在最后一页/中间页时,我也希望相同的中间子页。但这应该由自己知道如何迈出第一步。

我所有的想法都失败了。有人对这样的解决方案有想法吗?

完整的工作代码在这里:

<?php

 
 if( is_singular() )
     return;

 global $wp_query;

 /** Stop execution if there's only 1 page */
 if( $wp_query->max_num_pages <= 1 )
     return;

 $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
 $max   = intval( $wp_query->max_num_pages );

 /** Add current page to the array */
 if ( $paged >= 1 )
     $links[] = $paged;

 /** Add the pages around the current page to the array */
 if ( $paged >= 3 ) {
     $links[] = $paged - 1;
     $links[] = $paged - 2;
 }

 if ( ( $paged + 2 ) <= $max ) {
     $links[] = $paged + 2;
     $links[] = $paged + 1;
 }

 echo '<div class="navigation"><ul>' . "\n";

 /** Previous Post Link */
 if ( get_previous_posts_link() )
     printf( '<li class="prev_post">%s</li>' . "\n", get_previous_posts_link('<') );

 /** Link to first page, plus ellipses if necessary */
 if ( ! in_array( 1, $links ) ) {
     $class = 1 == $paged ? ' class="active"' : '';

     printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

     if ( ! in_array( 2, $links ) )
         echo '<li>…</li>';
 }

 /** Link to current page, plus 2 pages in either direction if necessary */
 sort( $links );
 foreach ( (array) $links as $link ) {
     $class = $paged == $link ? ' class="active"' : '';
     printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
 }

 /** Link to last page, plus ellipses if necessary */
 if ( ! in_array( $max, $links ) ) {
     if ( ! in_array( $max - 1, $links ) )
         echo '<li >…</li>' . "\n";

     $class = $paged == $max ? ' class="active"' : '';
     printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
 }

 /** Next Post Link */
 if ( get_next_posts_link() )
     printf( '<li class="next_post">%s</li>' . "\n", get_next_posts_link('>') );

 echo '</ul></div>' . "\n";

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    一般的分页代码流可能如下所示:

    <?php
    
    function getPageDisplayArray(int $currentPage, int $totalCount, int $pageLen = 10): array
    {
       $paginationArr[$currentPage] = 1;
       $totalPagesToDisplay = floor(($totalCount) / $pageLen);
       
       //Get preceding pages logic
       $pagesBefore = $currentPage - 1;
       $iBefore = 2; //Number of pages you want befpre the current page
       while($pagesBefore > 0 && $iBefore > 0) { 
           $paginationArr[$pagesBefore--] = 0;
           $iBefore--;
       }
    
       //Get anteceding pages
       $pagesAfter = $totalPagesToDisplay - ($pagesBefore + 1);
       $iAfter = min([$pagesAfter, 2]); //Number of pages you want after your current page
       while(-$iAfter < 0) { 
           //Either return the two pages after or don't if there are no pages after
           $paginationArr[$iAfter + $currentPage] = 0;
           $iAfter--;
       }
    
       if($pagesAfter > 2) {
           //Calcluate Midpoint
           $midpoint = floor($totalPagesToDisplay / 2) + 1;
           if(!isset($paginationArr[$midpoint])) {
               $paginationArr[$midpoint] = 0;
           }
       }
       //Link the first page always if it hasn't already been set
       if(!isset($paginationArr[1])) {
           $paginationArr[1] = 0;
       }
    
       //Link the final page if it has not already been set.
       if(!isset($paginationArr[$totalPagesToDisplay])) {
          $paginationArr[$totalPagesToDisplay] = 0;
       }
       ksort($paginationArr);
       return $paginationArr;
    }
    
    

    当前页面、页面长度和总计数是您的参数。这里将在两侧显示两个页面作为显示页面,以及任何 arbitrart 集合的集合的第一页、最后一页和中间页。这将返回一个配置数组,该数组将“活动”页面的值设置为 1,而所有其他页面的页码键和值设置为 0。剩下的就是在布局中显示您认为合适的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2012-10-28
      • 1970-01-01
      • 2014-05-08
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多