• Hey all,

    I have created a simple theme using Twitter Bootstrap and am running into a couple issues with pagination.

    Issue 1
    In every instance pagination shows up, it displays more pages than actually exist. Here are a few instances that I’m referring to:

    You get the idea.

    Issue 2
    Pagination disappears on the last page of the content. Example: https://www.beerbitty.com/recipes/condiments/page/2/. Pagination is present on page 1, but disappears on page 2.

    Code
    Here’s what I have going on in my functions file:

    function bootstrap_pagination($pages = '', $range = 2)
    {
    $showitems = ($range * 2)+1;
    
    global $paged;
    if(empty($paged)) $paged = 1;
    
    if($pages == '')
    {
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
    $pages = 1;
    }
    }
    
    if(1 != $pages)
    {
    echo "<ul class='pagination pagination-centered'>";
        /**	Previous Post Link */
    	if ( get_previous_posts_link() )
    		printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
    
    for ($i=1; $i <= $pages; $i++)
    {
    if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    {
    echo ($paged == $i)? "<li class='active'><span class='current'>".$i."</span></li>":"<li><a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a></li>";
    }
    }
    
    /**    Next Post Link */
    	if ( get_next_posts_link() )
    		printf( '<li>%s</li>' . "\n", get_next_posts_link() );
    echo "</ul>\n";
    }
    }
    
    add_theme_support('html5', array('search-form'));
    
    if (!current_user_can('manage_options')) {
    	add_filter('show_admin_bar', '__return_false');
    }

    And here’s what my template file looks like:

    <?php /*
    Template Name: Category
    */ ?>
    
    <?php get_header(); ?>
    
    <div class="container">
      <div class="row">
        <div class="col-sm-4">
          <?php get_sidebar();?>
        </div><!-- /.col -->
        <div class="col-sm-8">
          <div class="row">
            <div class="col-sm-12">
              <h1>Recipes: <?php the_title(); ?></h1>
              <hr>
            </div><!-- /.col -->
            <?php
              $qry[1] = 2;
              $qry[2] = 2;
              $qry[3] = 2;
              $qry['total'] = array_sum($qry);
              $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
              $off = ($paged > 1) ? ($qry['total'] * ($paged - 1)) : 0;
              global $more;
              $more = 0;
              $page_cat = $post_slug=$post->post_name; //copy the page title
              $page_cat = strtolower($page_cat); // make it lower case only
              $my_query = new WP_Query('category_name='.$page_cat);
    
            ?>
            <div class="row">
              <div class="col-sm-12">
                <?php query_posts( "posts_per_page=$qry[total]&paged=$paged&showposts=$qry[1]&offset=$off&category_name='.$page_cat.'" );
                  if (have_posts()) :
                  while (have_posts()) : the_post();
                ?>
                <div class="col-sm-6">
                  <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); ?>
                  <a href="<?php the_permalink(); ?>"><img src="<?php echo $url ?>" class="img-responsive blogImage" /></a>
                  <h2 class="no-margin-bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                  <p class="no-margin-top no-margin-bottom text-muted"><?php the_time('m.d.y'); ?></p>
                  <p class="excerpt"><?php $excerpt = strip_tags(get_the_excerpt()); echo $excerpt; ?></p>
                  <p class="readMore pull-right"><a href="<?php echo get_permalink(); ?>">Read More <i class="fa fa-angle-double-right"></i></a></p>
                </div><!-- /.col -->
                <?php endwhile; else: ?>
                <?php endif; ?>
              </div><!-- /.col -->
            </div><!-- /.row -->
            <div class="row">
              <div class="col-sm-12">
                <?php $a = ($qry['total'] * ($paged - 1)); $off = $qry[1] + (($paged > 1) ? $a : 0); query_posts( "posts_per_page=$qry[total]&paged=$paged&showposts=$qry[2]&offset=$off&category_name='.$page_cat.'" ); if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div class="col-sm-6">
                  <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); ?>
                  <a href="<?php the_permalink(); ?>"><img src="<?php echo $url ?>" class="img-responsive blogImage" /></a>
                  <h2 class="no-margin-bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                  <p class="no-margin-top no-margin-bottom text-muted"><?php the_time('m.d.y'); ?></p>
                  <p class="excerpt"><?php $excerpt = strip_tags(get_the_excerpt()); echo $excerpt; ?></p>
                  <p class="readMore pull-right"><a href="<?php echo get_permalink(); ?>">Read More <i class="fa fa-angle-double-right"></i></a></p>
                </div><!-- /.col -->
                <?php endwhile; else: ?>
                <?php endif; ?>
              </div><!-- /.col -->
            </div><!-- /.row -->
    <div class="row">
              <div class="col-sm-12">
                <?php $a = ($qry['total'] * ($paged - 1)); $off = ($qry[1] + $qry[2]) + (($paged > 1) ? $a : 0); query_posts( "posts_per_page=$qry[total]&paged=$paged&showposts=$qry[3]&offset=$off&category_name='.$page_cat.'" ); if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div class="col-sm-6">
                  <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); ?>
                  <a href="<?php the_permalink(); ?>"><img src="<?php echo $url ?>" class="img-responsive blogImage" /></a>
                  <h2 class="no-margin-bottom"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                  <p class="no-margin-top no-margin-bottom text-muted"><?php the_time('m.d.y'); ?></p>
                  <p class="excerpt"><?php $excerpt = strip_tags(get_the_excerpt()); echo $excerpt; ?></p>
                  <p class="readMore pull-right"><a href="<?php echo get_permalink(); ?>">Read More <i class="fa fa-angle-double-right"></i></a></p>
                </div><!-- /.col -->
                <?php endwhile; else: ?>
                <?php endif; ?>
              </div><!-- /.col -->
            </div><!-- /.row -->
          </div><!-- /.row -->
          <?php bootstrap_pagination();?>
        </div><!-- /.col-sm-8 -->
      </div><!-- /.row -->
    </div><!-- /.container -->
    
    <?php get_footer(); ?>

    Other notes

    • I’ve adjusted the “Blog pages show at most” option in the Reading settings to 6.
    • I’ve spent a lot of time searching for ways to solve these problems, but haven’t been able to find a solution.
    • My PHP knowledge is pretty weak…
    • I’m really stuck!

    Thanks in advance for your help!!!

  • The topic ‘Pagination displaying more pages than exist’ is closed to new replies.