Home

Skip to Content Skip to Navigation

WP Pagination Tip

October 20th, 2007

Also known as 'Wordpress Tips: Pagination on secondary queries'. The quick and dirty way.

I'm working on using Wordpress as a CMS, and I've been making a 'News' page which the client can update. Basically, 'News' is a post category, and there are two columns of the page on the front end. The left column has titles and short decscriptions the most recent 4 posts from the 'News Category', while the right/middle column has the full version of the current news item being viewed. I did this with two queries: the first being the middle colum which was just a standard version of The Loop, and the second a custom query using the WP_Query object.

The problem came when I wanted to setup a pagination system for the left column. Normally the procedure is to use the in-built functions such as next_posts_link(), but I had a sneaky suspicion that this would only work with one Loop, and not on a little side query. So I had to find a quick and dirty solution (due to deadlines) so I decided to put a PHP variable in the URL, not exactly pretty, but as far I could tell, it was essential. The variable was used as an offset i.e. the page would load 4 posts starting from offset X.

First off, I'll print out the code I used at the bottom of the side query.:

$url = get_bloginfo('url');
$url = substr($url, 0, -4);
$url .= $_SERVER['REQUEST_URI'];

$count = $query->post_count;

# the variable used was called off, and the offset I used was 4

if(!isset($_GET["off"]) && $count == 4)
{
$url = $url.'?off=4';
<code><a title="Previous 4 Stories" href="<?=$url?>">Older News</a></code>

}
else
{
$curroff = '?off='.$_GET["off"];

$prevoff = $_GET["off"] + 4;
$prevoff = '?off='.$prevoff;
$prevurl = str_replace($curroff, $prevoff, $url);

$nextoff = $_GET["off"] - 4;
$nextoff = '?off='.$nextoff;
$nexturl = str_replace($curroff, $nextoff, $url);

if($count == 4)
{

<a title="Previous 4 Stories" href="http://www.red-root.com/wp-admin/%3C?=$prevurl?%3E">Older News</a>
}

if($_GET["off"] != 0)
{
<a title="Next 4 Stories" href="http://www.red-root.com/wp-admin/%3C?=$nexturl?%3E">Newer News</a>
}
}
?>

What happens is that a URL is generated, $url, which contains the post name, plus the '?off=x' bit. I used str_replace() in order to take out the current off variable instead of using substr() because the offset would go into any number of figures.

This makes a bit more sense if you see the WP_Query object I used at the top


if(isset($_GET["off"]) == true)
{
$query = new WP_Query('cat=1&showposts=4&offset='.$_GET["off"]);
}
else
{
$query = new WP_Query('cat=1&showposts=4');
}

3 Responses to WP Pagination Tip

Avatar

Jb

March 15th, 2008 at 12:03 pm

Hello,

I tried to use your hack, as i need it for the same purpose on my website, but I got a nice PHP error:
Parse error: syntax error, unexpected $end in /home/jbsiteco/public_html/wp-content/themes/jbtheme/index.php on line 83
(line 83 which is the end of the index file)

Would it be possible for you to send me your whole file by mail?

Thanks for your attention.

Avatar

Luke

March 15th, 2008 at 6:06 pm

Yeah sure go ahead, the code posted is a bit wrong, my code highlighter is acting funny, but i’ll take a look at it.

Avatar

Quotes Many

December 27th, 2009 at 6:33 pm

Hey, Its really useful one, I am gonna try it for my new site http://www.quotesmany.com

Thanks buddy!

Comment Form

Back to top