Wordpress上的分页问题

时间:2018-01-16 19:33:53

标签: wordpress pagination

关于分页有很多问题......我已经倾注了它们,并将以下代码放在一起(见下文)。问题:某些类别页面没有显示任何结果(只有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;

1 个答案:

答案 0 :(得分:1)

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

Category.php Loop =

&#13;
&#13;
<?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 } ?>
&#13;
&#13;
&#13;

然后你的分页功能:

&#13;
&#13;
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;
        }
	}
}
&#13;
&#13;
&#13;

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