HTML 没有插件的分页WordPress

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML 没有插件的分页WordPress相关的知识,希望对你有一定的参考价值。

function paglink($first=1,$last=1,$middle=10,$baseURL=false,$wp_query=false ) {
       if(!$baseURL) $baseURL= get_bloginfo('url');
       if(!$wp_query)global $wp_query;
       $page = $wp_query->query_vars["paged"];
       if ( !$page ) $page = 1;
       $qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";
       if ( $wp_query->found_posts > $wp_query->query_vars["posts_per_page"] ) {
               echo '<div class="box-paginacao">';
               if ( $page > 1 ) { 
                       echo '<a href="'.$baseURL.(($page==2)?('page/'.($page-1).'/'):'').$qs.'" class="link-anterior">anterior</a>';
               }
			   else {
					echo '<div class="link-anterior-sem">anterior</div>'; 
			   }
			   
			   
               $dots=false;
			   
			   echo '<div class="paginas">';
               for ( $i=1; $i <= $wp_query->max_num_pages; $i++ ){ // Loop through pages
                       if($i<=$first || $i<=$middle && $page<$middle || $i>$wp_query->max_num_pages-$last || $i>$wp_query->max_num_pages-$middle && $page>$wp_query->max_num_pages-$middle+1 || $i>$page-ceil($middle/2) && $i<=$page+floor($middle/2)){
                               if ( $i == $page ) { // Current page or linked page?
                                       echo '<strong>'.$i.'</strong>';
                               } else {
                                       echo '<a href="'.$baseURL.(($i!=1)?('page/'.$i.'/'):'').$qs.'">'.$i.'</a>';
                               }
                               $dots=false;
                       }elseif(!$dots){
                               echo '<span>...</span>';
                               $dots=true;
                       }
               }
			   
			    echo '</div>';
			   
               if ( $page < $wp_query->max_num_pages ) { // Next link?
                       echo '<a href="'.$baseURL.'page/'.($page+1).'/'.$qs.'" class="link-proximo">próximo</a>';
               }
			   else {
					 echo '<div class="link-proximo-sem">próximo</div>';   
			   }
               echo '</div>';
       }
}

//use function
paglink(1, 1, 10, get_bloginfo('wpurl').'/your_post_type_or_categories/', $wp_query);

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');

以上是关于HTML 没有插件的分页WordPress的主要内容,如果未能解决你的问题,请参考以下文章

PageHelper和MybatisPlus的分页插件冲突

wordpress中自定义帖子类型的分页

php Wordpress中的分页

基于angular的分页组件插件,使用directive,将directive的内容放到js文件中,直接引用即可

基于VUE2.0的分页插件(很好用,很简单)

Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)