How to: Integrate a pagination in your WordPress theme
Posted by jbj on Sep 30, 2008 in wordpress • 31 commentsWhen 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.
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" />

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?















Defiantly something worth introducing to your blog. One that I’ve found which is quite good (and a touch easier) is WP Page Numbers plugin. The only problem is i don’t think it’s part of the Wordpress plugins repository so i think you need to check for new version..
http://www.jenst.se/2008/03/29/wp-page-numbers is the url if anyone is interested
Wonderful post. I was looking for these code for like ages and i found it here. Thanks for sharing it with us. It really helped me a lot
@Jamie: I didn’t knew WP Page Number, I’ll chck it out!
@Alex: Thanks for your kind words! I’m glad you enjoyed this post!
it’s especially great for blogs with a lot of content because the pagination shows this, via the number of pages.
It was actually just yesterday that my friend was asking me if I knew all about Word Press.. the answer was no.. and now it´s getting more towards yes. I´ll pass this on to her and hope it helps her out!
Marina
Awesome thanks these codes i’m sure in the near future will prove to be very helpful!
Thanks for taking the time to put them up there for people like us…
Thanks,
Ryan
I’m currently using Digg-pagination plugin and I presume they both works the same, do they? Thanks for showing us the alternative.
If given a choice of embedding or plugin, it’s a no brainer. All themes should have page pagination integrated. Now, anyone knows whether there’s a way to embed the “Notify me of followup comments via email”……
@Neowster: I’m agree that all theme should have an integrated pagination, and WordPress should provide some core functions to do it. But the principe of plugin is very good too: You can install just what you want/need.
@Blogging for Bloggers: They probably works the same in theory, but in practise, I don’t know.
thanks thats very useful. i tried before with plugin, but this way is better. I will share on my Turkish blog.
@mutena: Glad you enjoyed this hack. You’re welcome to share it on your blog!
Very detailed tutorial! I believe it is going to be of some help here.. Thanks dude
Adding a pagination to a theme sure does feel like an optimized way of adding an element to it. Not having to have a plug-in installed is a definite plus, and is appreciated by the viewer.
Wonderful piece of code for your blog! Thanks.
a simple tutorial , that kind of pagination is suitable for blog with so many posts… it will simplify the blog struckture … but hacking that plugins was risky for newbie
But for web designer I think it is good to create premium complete themes with above hacked plugins pre installed in themes :0
Thanks for the tip. I never thought of doing this
Very useful tip here, thanks for sharing!