I am having an issue with pagination on a site I am working on. The client noticed that on her category pages, the pagination does not function after page 1. What I mean is that on pages 2 and onwards the posts no longer display from the WordPress loop. I was just wondering why this was and how I can fix it?

Here is my template page code for the category pages:

<?php
get_header();?>
<div class="section blog">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-md-8">
                <div class="share">
                        <span>Share:</span>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-twitter fa-stack-1x"></i>
                            </span>
                        </a>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-facebook fa-stack-1x"></i>
                            </span>
                        </a>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-linkedin fa-stack-1x"></i>
                            </span>
                        </a>
                </div>
                        <?php

                            $link = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
                            $link_array = explode('/',$link);
                            $page = $link_array[count($link_array)-2];

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

                            $args = array(
                            'post_type' => 'post',
                            'posts_per_page' => 4,
                            'paged' => $paged,
                            'category_name' => $page
                            );

                            $post_query = new WP_Query( $args );

                            if($post_query->have_posts() ) {
                                while($post_query->have_posts() ) {
                                    $post_query->the_post();
                                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
                                    if($image[0] == ''){
                                        $image[0] = get_template_directory_uri() . '/img/placeholder-blog.jpg';
                                    }
                        ?>

                <article>
                    <div class="post">
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <div class="head">  
                                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                    <span><?php the_time('F j, Y'); ?> by <?php echo get_the_author(); ?> | <a href="<?php the_permalink(); ?>">Leave a Comment</a></span>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 col-md-3 feature-image">
                                <img src="<?php echo $image[0]; ?>" alt="">
                            </div>
                            <div class="col-xs-12 col-md-9">
                                <p><?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...'); ?></p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <div class="category">
                                    <p>
                                        Filed Under: 
                                        <?php
                                            $category = get_the_category();
                                            foreach($category as $key=>$cat){
                                                echo '<a href="' . get_site_url() . '/blog/' . $cat->slug . '">' . $cat->name . '</a>';
                                                if(($key+1) != count($category)){
                                                    echo ', ';
                                                }
                                            }
                                        ?>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </article>
                <hr>
                        <?php
                                }
                            }
                        ?>


                        <div class="section nav text-center">
                            <!-- pagination here -->
                            <?php
                                if (function_exists(custom_pagination)) {
                                custom_pagination($custom_query->max_num_pages,"",$paged);
                                }
                            ?>

                        </div>


            </div>
            <div class="col-xs-12 col-md-4">
                <?php include('inc/sidebar.php'); ?>
            </div>
        </div>
    </div>
</div>
<?php 
include('inc/connect.php');
get_footer(); 
?>

There is also a custom function built into functions.php for pagination that perhaps contains as issue as well:

function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   * 
   * It's good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('<i class="fa fa-angle-left"></i>'),
    'next_text'       => __('<i class="fa fa-angle-right"></i>'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo $paginate_links;
    echo "</nav>";
  }

}

Here are live examples of such:

https://www.staceybarr.com/measure-up/category/podcast/page/2/
https://www.staceybarr.com/measure-up/category/podcast/page/1/

Everything looks correct to me but alas there is still an issue, I was just wondering if I could get some help with this? Any help is greatly appreciated!

up vote 2 down vote accepted

I'm not sure why the first page works but the second doesn't, though this may be a hint toward the problem. Toward the bottom of the category template page you have the following:

if (function_exists(custom_pagination)) {
    custom_pagination($custom_query->max_num_pages,"",$paged);
}

If you review the above, there is a variable called $custom_query. This looks like it's a WordPress query that should be able to have a max_numb_pages item inside it. The issue is $custom_query doesn't exist anywhere else on the page and doesn't appear to be global. You should change it to reflect your query on the page, $post_query. The updated code would look like this:

if (function_exists(custom_pagination)) {
    custom_pagination($post_query->max_num_pages,"",$paged);
}

This should tell the pagination that there are a number of pages and possibly render the correct link.

Further, I would recommend you print_r the output of $link, $link_array, $page, and $paged to determine if all of those variables are outputting the correct response.

$link = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$link_array = explode('/',$link);
$page = $link_array[count($link_array)-2];

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

$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'paged' => $paged,
'category_name' => $page
);

print_r($link);
print_r($link_array);
print_r($page);
print_r($paged);
print_r($args);

In the case of $args, make sure it's telling WordPress the correct page in the paged attribute.

  • Thank you for the quick response! When I change the $custom_query to $post_query, it starts to act funny and the pagination displays an incorrect number of page links. For example if they are 2 pages, it will display 5 pages with $post_query. I used the print_r and the variables are outputting the correct responses. It still has the same issue with posts not being displayed after the first page how ever! – NeoLobster Oct 18 '17 at 18:55
  • I spoke too soon Tom; I noticed that on page 2+ of any category pages the category name will change from what it is, to "2", "3", etc. depending on what page you are on. For example on: staceybarr.com/measure-up/category/podcast, it renders: codewww.staceybarr.com/measure-up/category/podcast/Array ( [0] => www.staceybarr.com [1] => measure-up [2] => category [3] => podcast [4] => ) podcast1Array ( [post_type] => post [posts_per_page] => 4 [paged] => 1 [category_name] => podcast )code – NeoLobster Oct 18 '17 at 19:11
  • While on staceybarr.com/measure-up/category/podcast/page/2, it renders: codewww.staceybarr.com/measure-up/category/podcast/page/2/Array ( [0] => www.staceybarr.com [1] => measure-up [2] => category [3] => podcast [4] => page [5] => 2 [6] => ) 22Array ( [post_type] => post [posts_per_page] => 4 [paged] => 2 [category_name] => 2 )code I was just wondering what I could do to correct the mismatched category names – NeoLobster Oct 18 '17 at 19:13
  • @NeoLobster Category name is coming from the $page variable, which is attempting to pull from the URL broken into an array with $link_array. If you notice, the array key for category is always 3, so you may want to change $page in the $args from what it is to $link_array[3] and see if that works. – Tom Oct 18 '17 at 19:42
  • 1
    Thanks so much Tom! When I changed the value of $page to be $link_array[3] the pagination started to work perfectly! I very much appreciated all of your help! – NeoLobster Oct 18 '17 at 20:48

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.