php Wordpress中的分页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 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');

以上是关于php Wordpress中的分页的主要内容,如果未能解决你的问题,请参考以下文章

WordPress 中自定义 wp_query 的分页需要 404 错误页面

HTML 没有插件的分页WordPress

php中的分页显示

自定义archive.php页面中的分页

WordPress 分页 - 添加锚链接

scss WordPress的分页与页码和一个很酷的sass循环。