WP Pagination Tip

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.:

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

« Graphic Pieces The World Cup is over … »

Comments

  1. Jb | March 15th, 2008 | 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.

  2. Luke | March 15th, 2008 | 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.

Post a comment

RSS Icon RSS Feed | hCard Icon Download vCard | CV Icon Download CV | Going up?
© Luke Williams / Red Root 2008
 

It seems you're using an unsafe, out-of-date browser. Click here to upgrade to Firefox for free. X