Advertisement
salmancreation

textdomain_paging_nav() - custom paging nav

Apr 29th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. if ( !function_exists( 'textdomain_paging_nav' ) ) {
  2.  
  3.     function textdomain_paging_nav() {
  4.  
  5.  
  6.         if ( is_singular() )
  7.             return;
  8.  
  9.         global $wp_query;
  10.  
  11.         /** Stop execution if there's only 1 page */
  12.         if ( $wp_query->max_num_pages <= 1 )
  13.             return;
  14.  
  15.         $paged   = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
  16.         $max     = intval( $wp_query->max_num_pages );
  17.  
  18.         /**     Add current page to the array */
  19.         if ( $paged >= 1 )
  20.             $links[] = $paged;
  21.  
  22.         /**     Add the pages around the current page to the array */
  23.         if ( $paged >= 3 ) {
  24.             $links[] = $paged - 1;
  25.             $links[] = $paged - 2;
  26.         }
  27.  
  28.         if ( ( $paged + 2 ) <= $max ) {
  29.             $links[] = $paged + 2;
  30.             $links[] = $paged + 1;
  31.         }
  32.  
  33.         echo '<ul class="pagination justify-content-center">' . "\n";
  34.  
  35.         /**     Previous Post Link */
  36.         if ( get_previous_posts_link() )
  37.             printf( '<li>%s</li>' . "\n", get_previous_posts_link( '<i class="fa fa-long-arrow-left"></i>' ) );
  38.  
  39.         /**     Link to first page, plus ellipses if necessary */
  40.         if ( !in_array( 1, $links ) ) {
  41.             $class = 1 == $paged ? ' class="active"' : '';
  42.  
  43.             printf( '<li%s><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
  44.  
  45.             if ( !in_array( 2, $links ) )
  46.                 echo '<li>…</li>';
  47.         }
  48.  
  49.         /**     Link to current page, plus 2 pages in either direction if necessary */
  50.         sort( $links );
  51.         foreach ( (array) $links as $link ) {
  52.             $class = $paged == $link ? ' class="active"' : '';
  53.             printf( '<li%s><a class="page-link" href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
  54.         }
  55.  
  56.         /**     Link to last page, plus ellipses if necessary */
  57.         if ( !in_array( $max, $links ) ) {
  58.             if ( !in_array( $max - 1, $links ) )
  59.                 echo '<li>…</li>' . "\n";
  60.  
  61.             $class = $paged == $max ? ' class="active"' : '';
  62.             printf( '<li%s><a href="%s" class="page-link">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
  63.         }
  64.  
  65.         /**     Next Post Link */
  66.         if ( get_next_posts_link() )
  67.             printf( '<li>%s</li>' . "\n", get_next_posts_link( '<i class="fa fa-long-arrow-right"></i>' ) );
  68.  
  69.         echo '</ul>' . "\n";
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement