为了账号安全,请及时绑定邮箱和手机立即绑定

添加过渡页面到分页

添加过渡页面到分页

PHP
慕莱坞森 2023-07-15 17:19:07
现在,当有 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 ); }
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

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


<?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;

}


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


查看完整回答
反对 回复 2023-07-15
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信