How to: Integrate a pagination in your WordPress theme

How to: Integrate a pagination in your WordPress theme

Posted by Jean-Baptiste Jung on Sep 30, 2008 | 74 comments

When creating a premium WordPress theme, it is really better to use a pagination instead of the good old “previous page” and “next page” links. Sure, there’s plugins, but what about embeding a pagination without asking the user to install a plugin? In this tutorial, I’ll show you how to directly integrate the Wp-PageNavi plugin in your WordPress theme.

Author : Jean-Baptiste Jung

Jean is a 27 years old self-taught web developer and WordPress specialist who lives in Wallonia, the French speaking part of Belgium. In addition to Cats Who Code, he also blogs about WordPress at WpRecipes and about Photoshop at PsdVibe.

WP-PageNavi

WP-PageNavi is a WordPress plugin, created by Lester Chan, in order to display a page navigation instead of the "previous" and "next" pages links.

Of course, the first thing to do is to download the plugin. Once you have it on your hard drive, unzip it, then copy and paste the following files to your theme directory:

  • wp-pagenavi.php
  • wp-pagenavi.css

Editing the files

Open the file where you want the pagination to be displayed (index.php, categories.php or search.php).
Find the following code:

<div class="navigation">
	<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
	<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
</div>

Replace this part by the code below:

<?php
include('wp-pagenavi.php');
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
?>

If you save the file and refresh the theme page now, nothing will happen. This is just because Wp-PageNavi is a WP plugin and have not been created to be embeded in a theme file.
X-OR found the following hack. Just open the wp-pagenavi.php file and find the following code (Line 61):

function wp_pagenavi($before = '', $after = '') {
        global $wpdb, $wp_query;

We have to call the pagenavi_init() function, so let's do it that way:

function wp_pagenavi($before = '', $after = '') {
	global $wpdb, $wp_query;
        pagenavi_init(); //Calling the pagenavi_init() function

Now, refresh your theme file: The pagination is displayed well, but it don't look that good. We just forgot to import the related CSS file.

Open your header.php file and find the following line:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

Simply add the following code below:

<link rel="stylesheet" href="<?php echo TEMPLATEPATH.'/pagenavi.css';?>" type="text/css" media="screen" />

Embeding a pagination in your WordPress theme

Our pagination is now fully functionnal!

Embeding a pagination in your theme, or using a plugin?

I'm pretty sure than some of you wonders why I'm using this hack instead of simply using a plugin the "normal" way. This is simple: On CatsWhoCode, we use the plugin, because we're good enought in WordPress and web languages to integrate it well. But when I'm designing a WordPress theme (I'm creating two new themes theses days) I think about bloggers who aren't IT professionals. I think about all theses people who'd like to have a pagination embeded in their theme, but don't know how to do, even if installing a plugin is simple, at least for us.

When creating a premium WordPress theme, I tend to think that the designer/developper must do anything for the client to have less to do, so this is why I'm using this hack in the two themes I'm currently creating.

And you, any thoughts about that hack?

74 reactions

» Comments RSS Feed

Trackbacks

  1. Customize your WordPress Theme | Neowster
  2. WordPress: Integrate a pagination in your theme | bloground.ro - Blogging resources, WordPress themes and plugins for your development
  3. pligg.com
  4. New Free WordPress Theme from Cats Who Code
  5. New Free WordPress Theme from Cats Who Code | bloground.ro - Blogging resources, WordPress themes and plugins for your development
  6. New Free WordPress Theme from Cats Who Code | Wordpress Blog Services
  7. Free WordPress Theme Courtesy of Cats Who Code | Radyoplay IP-TV Technologie
  8. How to define how many post to display per page
  9. $ The use of pagenavi plugin $ - blogging and scraping
  10. WordPress News & Notes - October 23, 2008 | WordPress Hacks
  11. WordPress News &amp; Notes - October 23, 2008 | Wordpress Blog Services
  12. pagination in WordPress
  13. WordPress News & Notes - October 23, 2008 | Radyoplay IP-TV Technologie
  14. DivageekDesigns.com » Blog Archive » How to Add Social Networking Icons to your Blog
  15. WordPress News | Blog n' Ads
  16. 10 Killer WordPress Hacks | How-To | Smashing Magazine
  17. Wordpress Blog Services - 10 Killer WordPress Hacks
  18. 爱享VIP生活 » 10个杀手级WordPress技巧
  19. 10 Killer WordPress Hacks « ArticleSave
  20. 10 Killer WordPress Hacks | How2Pc
  21. Aurigis.com » Blog Archive » 10 Killer WordPress Hacks
  22. 10 Killer WordPress Hacks « DesignDevoted
  23. 10 Killer WordPress Hacks | Web Hosting and Domains
  24. 10 Killer WordPress Hacks | The Blog Specialist
  25. Mellowish » Blog Archive
  26. 10 Killer WordPress Hacks | SuperBlog
  27. 10个杀手级WordPress技巧(转) | 上网这点事
  28. KurtQian’s blog - 10 Killer WordPress Hacks
  29. Pagination in your WordPress theme | greybucket.com
  30. todo: add pagination to this wordpress blog - WordPress Test
  31. Complete Wordpress Theme Tutorial « Kolmex
  32. 6 วิธีที่จะทำให้ Wordpress เจ๋งขึ้น • Mekz Connecting
  33. Desing by » Añadir ¨paginacion¨ a un blog de WP
  34. Mon-Wordpress.fr » Comment insérer des liens de paginations dans votre thème
  35. Integrar paginación en tu theme WordPress sin plugin | Kernel Web

Leave a comment