添加过渡页面到分页

现在,当有 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 );

 }




慕莱坞森
浏览 55回答 1
1回答

开满天机

一般分页代码流可能如下所示:<?phpfunction getPageDisplayArray(int $currentPage, int $totalCount, int $pageLen = 10): array{&nbsp; &nbsp;$paginationArr[$currentPage] = 1;&nbsp; &nbsp;$totalPagesToDisplay = floor(($totalCount) / $pageLen);&nbsp; &nbsp;&nbsp; &nbsp;//Get preceding pages logic&nbsp; &nbsp;$pagesBefore = $currentPage - 1;&nbsp; &nbsp;$iBefore = 2; //Number of pages you want befpre the current page&nbsp; &nbsp;while($pagesBefore > 0 && $iBefore > 0) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$paginationArr[$pagesBefore--] = 0;&nbsp; &nbsp; &nbsp; &nbsp;$iBefore--;&nbsp; &nbsp;}&nbsp; &nbsp;//Get anteceding pages&nbsp; &nbsp;$pagesAfter = $totalPagesToDisplay - ($pagesBefore + 1);&nbsp; &nbsp;$iAfter = min([$pagesAfter, 2]); //Number of pages you want after your current page&nbsp; &nbsp;while(-$iAfter < 0) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//Either return the two pages after or don't if there are no pages after&nbsp; &nbsp; &nbsp; &nbsp;$paginationArr[$iAfter + $currentPage] = 0;&nbsp; &nbsp; &nbsp; &nbsp;$iAfter--;&nbsp; &nbsp;}&nbsp; &nbsp;if($pagesAfter > 2) {&nbsp; &nbsp; &nbsp; &nbsp;//Calcluate Midpoint&nbsp; &nbsp; &nbsp; &nbsp;$midpoint = floor($totalPagesToDisplay / 2) + 1;&nbsp; &nbsp; &nbsp; &nbsp;if(!isset($paginationArr[$midpoint])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$paginationArr[$midpoint] = 0;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;//Link the first page always if it hasn't already been set&nbsp; &nbsp;if(!isset($paginationArr[1])) {&nbsp; &nbsp; &nbsp; &nbsp;$paginationArr[1] = 0;&nbsp; &nbsp;}&nbsp; &nbsp;//Link the final page if it has not already been set.&nbsp; &nbsp;if(!isset($paginationArr[$totalPagesToDisplay])) {&nbsp; &nbsp; &nbsp; $paginationArr[$totalPagesToDisplay] = 0;&nbsp; &nbsp;}&nbsp; &nbsp;ksort($paginationArr);&nbsp; &nbsp;return $paginationArr;}其中当前页面、页面长度和总计数是您的参数。这里将在两侧显示两个页面作为显示页面,以及任何仲裁集的第一页、最后一页和中间页。这将返回一个配置数组,该数组将“活动”页面的值设置为 1,并将所有其他页面的键设置为页码,并将值设置为零。剩下的就是按照您认为合适的方式在布局中显示值。
打开App,查看更多内容
随时随地看视频慕课网APP