Advertisement
darrenbachan

custom pagination function

Jun 13th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. // Custom Page Pagination
  2.         function wp_custom_pagination($args = [], $class = 'pagination') {
  3.            
  4.             global $wp_query;
  5.             $mid_size = 1; // default to 1 number each side
  6.  
  7.             // Check if we are on the first or last page
  8.             $current_page = get_query_var('paged');
  9.             if ( !$current_page || $current_page == $wp_query->max_num_pages ) {
  10.                 $mid_size = 1; // 1 numbers before/after the current page
  11.             }
  12.  
  13.             $args = wp_parse_args( $args, [
  14.                 'mid_size'           => $mid_size,
  15.                 'prev_next'          => false,
  16.                 'prev_text'          => __('Older posts', 'textdomain'),
  17.                 'next_text'          => __('Newer posts', 'textdomain'),
  18.                 'screen_reader_text' => __('Posts navigation', 'textdomain'),
  19.             ]);
  20.  
  21.             $links     = paginate_links($args);
  22.             $next_link = get_next_posts_link($args['next_text']);
  23.             $prev_link = get_previous_posts_link($args['prev_text']);
  24.             $template  = apply_filters( 'navigation_markup_template', '
  25.             <nav class="navigation %1$s" role="navigation">
  26.                 <div class="nav-links">%3$s<div class="page-numbers-container">%4$s</div>%5$s</div>
  27.             </nav>', $args, $class);
  28.  
  29.             echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link);
  30.  
  31.         }
  32.         add_filter('previous_posts_link_attributes', function()
  33.         {
  34.         return 'class="previous"';
  35.         });
  36.  
  37.         add_filter('next_posts_link_attributes', function()
  38.         {
  39.         return 'class="next"';
  40.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement