user5321787
user5321787

Reputation:

Pagination Issue on Wordpress

There are plenty of questions regarding pagination... I've poured through them, and put together the following code (see below). The problem: Some category pages aren't showing any results (a category with only 3 posts). Other's aren't showing all of the posts in that category (one category has 80 posts, but are only showing 60)...

Below is my code...

On my category.php page...

<?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; 
?>

and on my functions.php page...

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;

Upvotes: 0

Views: 558

Answers (1)

Jon Bennett
Jon Bennett

Reputation: 441

You don't have to WP query the category posts because your using the category.php file.

Category.php Loop:

<?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();
} elseif ( 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
} ?>

Then your pagination function:

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;
        }
    }
}

The post limit will come from what you set in your reading settings.

Upvotes: 1

Related Questions