• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Loading custom module plugins
    • Moved
    • I’ll be moving soon…
    • Quick Netbeans tip – task filters
    • Honeypot for Zend Framework
    • Toggler
    • Book review – jQuery 1.3 with PHP
    • 2009 in a few words
    • Bad Firebug!
    • Posterous
  • Recent Comments

    • Robert on Loading custom module plugins
    • Nikola Poša on Loading custom module plugins
    • Jani Hartikainen on Moved
    • Amar on Moved
    • Racinante on I’ll be moving soon…
    • Robert on I’ll be moving soon…
    • rizza on I’ll be moving soon…
    • vranac on I’ll be moving soon…
    • Alex on Online resources for Zend Framework
    • Robert on Online resources for Zend Framework
  • Tags

    about apache blog book comic error example facebook filter framework free freelance freelancing free software introduction jquery lamp licence linux me moving mysql navigation open source pcre php plugin project proprietary python random regexp registration review routing setup site svn trac twitter ubuntu virtualbox web wordpress zend
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Archives

    • July 2010
    • June 2010
    • April 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • August 2009
    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework

WordPress paging navigation

by Robert Basic on October 6th, 2008

As I’m not a big fan of WordPress plug—ins, and I wanted to use a normal page navigation, not just the default “Previous posts” and “Next posts”, I decided to play around a bit and create my own paging navigation, or pagination.

Preparation

An example of the navigation

An example of the navigation

First, I wrote on a piece of paper which links I need: first page, last page, next page, previous page and the links with the page numbers. Next, I needed to see what functions are already in WordPress, to reuse as much as I can. After a little searching, I found that the functions for the default navigation are located in the link-template.php file, under the wp-includes folder. There are the functions for the next and previous pages, and the function that creates the URL. Furthermore, I wanted a sliding pagination (like Yahoo has on it’s search page), ’cause it’s easy to use and looks cool.

The function

So, let’s take a look at the code. I called the function simply get_pagination; it’s quite self—describing. I put it in the link-template.php file, that way, all functions for navigation are in one place.

/**
* A pagination function
* @param integer $range: The range of the slider, works best with even numbers
* Used WP functions:
* get_pagenum_link($i) - creates the link, e.g. http://site.com/page/4
* previous_posts_link(' « '); - returns the Previous page link
* next_posts_link(' » '); - returns the Next page link
*/
function get_pagination($range = 4){
  // $paged - number of the current page
  global $paged, $wp_query;
  // How much pages do we have?
  if ( !$max_page ) {
    $max_page = $wp_query->max_num_pages;
  }
  // We need the pagination only if there are more than 1 page
  if($max_page > 1){
    if(!$paged){
      $paged = 1;
    }
    // On the first page, don't put the First page link
    if($paged != 1){
      echo "<a href=" . get_pagenum_link(1) . "> First </a>";
    }
    // To the previous page
    previous_posts_link(' « ');
    // We need the sliding effect only if there are more pages than is the sliding range
    if($max_page > $range){
      // When closer to the beginning
      if($paged < $range){
        for($i = 1; $i <= ($range + 1); $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo "class='current'";
          echo ">$i</a>";
        }
      }
      // When closer to the end
      elseif($paged >= ($max_page - ceil(($range/2)))){
        for($i = $max_page - $range; $i <= $max_page; $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo "class='current'";
          echo ">$i</a>";
        }
      }
      // Somewhere in the middle
      elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
        for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
          echo "<a href='" . get_pagenum_link($i) ."'";
          if($i==$paged) echo "class='current'";
          echo ">$i</a>";
        }
      }
    }
    // Less pages than the range, no sliding effect needed
    else{
      for($i = 1; $i <= $max_page; $i++){
        echo "<a href='" . get_pagenum_link($i) ."'";
        if($i==$paged) echo "class='current'";
        echo ">$i</a>";
      }
    }
    // Next page
    next_posts_link(' » ');
    // On the last page, don't put the Last page link
    if($paged != $max_page){
      echo " <a href=" . get_pagenum_link($max_page) . "> Last </a>";
    }
  }
}

The “range” is the range of the sliding effect, i.e. how many numbers are shown besides the current number: if the range is 4, and the current page is 5, then the numbers 3, 4, 5, 6 and 7 are visible.

Usage

It’s quite simple to use it: where the pagination is needed, just call the get_pagination() function, and it will show up. Add some CSS style to it, and your good to go.

Hope someone will find this useful :)

Tags: example, navigation, php, wordpress.
Categories: Development, Programming, Software.
Subscribe to the feed.

Comments: 16

Grab the comments feed

  • Nikos

  • October 21st, 2008

This is very helpful… But how can I call the get_pagination() ? Where exactly should I put it? Can you please be more specific? Im still new in the code of wordpress…

Thank you for the helpful code

  • Nikos

  • October 21st, 2008

Oh… and how exactly I will add the .css to it?

Thanx again

  • Robert

  • October 21st, 2008

Hi Nikos!

Call the get_pagination() in your template file, for example in index.php of your template. All I do is this:

<div class="pagination">
<?php get_pagination() ?>
</div>

Put the css styling where you want it, like in the css file of your template:

.navigation {
display: block;
text-align: center;
margin-top: 10px;
margin-bottom: 60px;
font-size:80%;
}
.navigation a{
text-align:center;
padding:5px 10px;
margin:0 2px 0 2px;
border:1px solid #6C7388;
}
.navigation a:hover{
background:#6C7388;
}

Hope this helps :)

  • eylultoprak

  • November 1st, 2008

Wooww.. Thanx again =)

  • Aktar

  • November 23rd, 2008

Hi Robert,
where is class=”pagination” ?

  • Nick

  • March 24th, 2009

This was very useful.

Thank you very much!

  • Bobby

  • April 1st, 2009

Huge help! Thanks so much!

  • Varun Sikka

  • April 29th, 2009

Thanks a lot for this. Its a huge help. It is amazing piece of code.
Thanks a ton once again…

  • Wordpress SEO paging navigation | Sulumits Retsambew

  • May 2nd, 2009

[...] available, so search engine spiders can hit the articles with the less steps. So I found this guy http://robertbasic.com/blog/wordpress-paging-navigation/ and his code. It works flawless and it is right what I needed. However since I like to have my [...]

  • Hellas

  • May 2nd, 2009

Hello. Excellent idea, but it has xhtml validation errors. I xhtml validated your script and you can download it here
http://www.sulumitsretsambew.org/wordpress-seo-paging-navigation/

Thanks one more time for this great piece of code.

  • luz

  • July 14th, 2009

excellent, congrats for the plugin!
I have a question,
how can I style the selected number, so users can know in wich page they are?

  • Shane

  • July 31st, 2009

Nice work.

  • adhie

  • November 4th, 2009

@luz add
.current{

background:#65ea00;

}
to ur css style.

@Aktar
change class=”pagination” to class=”navigation”

Thank you for the helpful code

  • Paginazione Articoli su Wordpress

  • November 13th, 2009

[...] La domanda come al solito è se abbiamo realmente bisogno di un plugin per la paginazione articoli. La risposta è NO poichè  abbiamo imparato che maggiori performance sono sinonimo (non sempre) di un basso uso dei plugins. Ecco perchè la soluzione di Robert Basic è vivamente consigliata. Sul suo sito web troviamo il codice da copiare nel file functions.php, dovremo successivamente darci da fare per personalizzare il tema dei pulsanti, cercando di allinearlo al layout del nostro sito web. Il link alla funzione di Robert Basic è http://robertbasic.com/blog/wordpress-paging-navigation/ [...]

  • Domen

  • December 17th, 2009

Great stuff. Works like a charm!

  • Jesse Rosenfield

  • April 23rd, 2010

Great work. Helped alot!

Leave a Reply

 

Robert Basic © 2008 — 2010
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS