【问题标题】:Pagination Issue on WordpressWordPress 上的分页问题
【发布时间】:2018-01-17 19:16:10
【问题描述】:

有很多关于分页的问题......我已经把它们倾倒了,并整理了以下代码(见下文)。问题:某些类别页面没有显示任何结果(只有 3 个帖子的类别)。其他的没有显示该类别中的所有帖子(一个类别有 80 个帖子,但只显示 60 个)...

下面是我的代码...

在我的 category.php 页面上...

<?php
    $currCat = get_category(get_query_var('cat'));
    $cat_name = $currCat->name;
    $cat_id   = get_cat_ID( $cat_name ); // Get cat ID

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    $args  = array(
        'meta_key' => 'ratings_average', // WP-Rating Plugin Rating Value
        'orderby' => 'meta_value_num', 
        'order' => 'DESC',
        'cat' => $cat_id,
        'posts_per_page' => 10,
        'paged' => $paged,
    );
    $wp_query = new WP_Query( $args ); 

    if ( $wp_query->have_posts() ) : 

        while ( $wp_query->have_posts() ) : $wp_query->the_post(); 
            get_template_part( 'entry' );
        endwhile; 

        //Pagination
        post_pagination();

    else:

        ?>Sorry, no results at this time.<?php

    endif; 
?>

在我的functions.php页面上...

if ( ! function_exists( 'post_pagination' ) ) :
   function post_pagination() {
     global $wp_query;
     $pager = 999999999; // need an unlikely integer

        echo paginate_links( array(
             'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
             'format' => '?paged=%#%',
             'current' => max( 1, get_query_var('paged') ),
             'total' => $wp_query->max_num_pages
        ) );
   }
endif;

【问题讨论】:

    标签: wordpress pagination


    【解决方案1】:

    您不必使用 WP 查询类别帖子,因为您使用的是 category.php 文件。

    Category.php 循环 =

    <?php if(have_posts()):
    
        while(have_posts()): the_post();
    
    
             get_template_part( 'entry' );
    
    
        endwhile;
    
    wp_reset_postdata();
    ?>
    
    <?php if ( function_exists('post_pagination') ) { post_pagination(); } else if ( is_paged() ) { ?>
    
    	  <ul class="pagination">
    		<li class="older"><?php next_posts_link('<i class="glyphicon glyphicon-arrow-left"></i> ' . __('Previous', 'theme')) ?></li>
    		<li class="newer"><?php previous_posts_link(__('Next', 'theme') . ' <i class="glyphicon glyphicon-arrow-right"></i>') ?></li>
    	</ul>
    
    <?php } ?>

    然后你的分页功能:

    if ( ! function_exists( 'post_pagination' ) ) {
    	function post_pagination() {
            global $wp_query;
            $big = 999999999; // This needs to be an unlikely integer
            $paginate_links = paginate_links( array(
                'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
                'current' => max( 1, get_query_var('paged') ),
                'total' => $wp_query->max_num_pages,
                'mid_size' => 5,
                'prev_next' => True,
                'prev_text' => __('<i class="fa fa-angle-double-left" aria-hidden="true"></i>'),
                'next_text' => __('<i class="fa fa-angle-double-right" aria-hidden="true"></i>'),
                'type' => 'list'
            ) );
            $paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
            $paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='active'><a href='#'>", $paginate_links );
            $paginate_links = str_replace( "</span>", "</a>", $paginate_links );
            $paginate_links = preg_replace( "/\s*page-numbers/", "", $paginate_links );
            // Display the pagination if more than one page is found
            if ( $paginate_links ) {
                echo $paginate_links;
            }
    	}
    }

    帖子限制将来自您在阅读设置中设置的内容,希望对您有所帮助

    【讨论】:

    • 他必须 WP_QUERY,因为他需要按自定义字段排序(基本 category.php 查询不这样做)
    • 是的,Jabbamonkey 说的。
    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 2021-07-31
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多