k-isabelle
5/31/2018 - 4:29 PM

Pagination in Wordpress

Pagination in Wordpress

1. Put this in functions.php:

<?php

/**
 * Pagination links for search and archives
 */
function get_pagination_links() {
    global $wp_query;
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $big = 999999999;

    return paginate_links( array(
        'base' => @add_query_arg('paged','%#%'),
        'format' => '?paged=%#%',
        'current' => $current,
        'total' => $wp_query->max_num_pages,
        'prev_next'    => true,
        'prev_text'    => __('« Previous Page'),
        'next_text'    => __('Next Page »'),
    ) );
}
?>

2. Put this in your template file:

<div class="navigation"><p><?php echo get_pagination_links(); ?></p></div>
<?php

  //setup new query
  $query = new WP_query( 
      array(
          'posts_per_page'    =>    10, // ??
          'post_type'            =>    'custom-post-type', // custom post type
          'paged' => get_query_var('paged'),
          'meta_key' => 'wpcf-meta-key',
          'orderby' =>  'meta_value title',
          'order' => ASC,
          'meta_value' => $meta_value,
          'location' => $currentLocation, // custom taxonomy
          'vendor-type' => $currentVendorType, // custom taxonomy
      )
  );
?>
<?php if ($query->have_posts()): ?>
  <?php while ($query->have_posts()) : $query->the_post(); ?>
    <!-- display posts -->
  <?php endwhile; ?>
<?php else: ?>
 <!-- fallback -->
<?php endif; ?>

<?php 
  $total_pages = $query->max_num_pages;  
  
  $args = array(
    'base'         => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%', 
    'format'       => 'page/%#%',
    'current'      => max( 1, get_query_var('paged') ),
    'total'        => $total_pages,  
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => False,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
    'add_args'     => array(
                       'vendor-type' => get_query_var('vendor-type'),
                       'vendor-location' => get_query_var('vendor-location')
                      ),
    'add_fragment' => ''
  ); 
?>
<?php if($total_pages > 1): ?>
  <nav class="vendor-nav group">
    <h1 class="hidden">Blog Post Navigation</h1>

    <span class="older">
      <?php next_posts_link('<div class="circle"><span class="arrow-e"></span></div>', $blush_vendor_query->max_num_pages) ?>
    </span>
    
    
    <span class="paginate-links">
      <?php echo paginate_links( $args ); ?>
    </span>

    <span class="newer">
      <?php previous_posts_link('<div class="circle"><span class="arrow-w"></span></div>', $blush_vendor_query->max_num_pages) ?>
    </span>
  </nav>
<?php endif; ?>

//----------------------------------------------
//  Limit Posts Per Archive Page
//----------------------------------------------

function limit_posts_per_archive_page() {
  if(!is_admin()){
    if ( is_category() )
      $limit = 2;
    if ( is_tag() )
      $limit = 2;
    elseif ( is_search() ) 
      $limit = 2;
    else
      $limit = get_option('posts_per_page');
  }
  set_query_var('posts_per_archive_page', $limit);
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');