Cynthia
Cynthia

Reputation: 5403

Why are Wordpress pagination links reloading the same page?

I have added pagination to a very basic custom theme. The links show up (page 1, page 2, etc...) but when I click on one of the links, it just reloads the same page.

Here is the code to add the pagination in the template:

<?php pagination($additional_loop->max_num_pages); ?>

and the function:

function 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 "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

If anyone can shed some light, I'd be most grateful!

Upvotes: 1

Views: 1560

Answers (1)

Mark
Mark

Reputation: 3055

paginate_links is a much easier way of adding pagination to your archive pages.

You need something like this, assuming you have permalinks enabled.

<?php

$ulpn=99999999; // Something bigger than you will ever have number of pages.
 $pagination=paginate_links(array('base'=>str_replace($ulpn,'%#%',esc_url(get_pagenum_link($ulpn))), 'format'=>'/page/%#%','current'=>max(1,get_query_var('paged')),'total'=>$wp_query->max_num_pages,'prev_text'=>'Previous','next_text'=>'Next','type'=>'array'));
  if($pagination) { ?>
    <div class="pagination">
      <?php foreach($pagination as $page) { echo $page; } ?>
    </div>
  <?php }

Upvotes: 1

Related Questions