Today I had a real problem with a WordPress website that uses multiple wp_query calls on the home page. The issue was that I could not use the next_posts_link() function properly, since WordPress ignores the “offset” parameter when creating paginated pages.
The solution I used was found here on the WordPress Codex.
Here is the code:
1) ? ($onum) : 0;
query_posts("posts_per_page=$qry[total]&paged=$paged&showposts=$qry[1]&offset=$offset");
if (have_posts()) :
while (have_posts()) : the_post();
// Loop stuff
endwhile;
endif;
// Reuse the offset variable (rather then creating a new one), setting to a new value (plus any additional offset)
// Total in first query, plus any offset, if there is one
$offset = $qry[1] + (($paged > 1) ? $onum : 0);
query_posts("posts_per_page=$qry[total]&offset=$offset&showposts=$qry[2]");
if (have_posts()) :
while (have_posts()) : the_post();
// Loop stuff
endwhile;
endif;
// Again just reusing the variable and adding offset (if page is more then 1)
// First posts total, plus second posts total, plus offset (if there is one)..
$offset = ($qry[1] + $qry[2]) + (($paged > 1) ? $onum : 0);
query_posts("posts_per_page=$qry[total]&offset=$offset&showposts=$qry[3]");
if (have_posts()) :
while (have_posts()) : the_post();
// Loop stuff
endwhile;
endif;
// Unset the variables (we've finished with them, no reason to hold onto them)
unset($qry,$offset,$onum);
?>
Can you think of an easier way to do this?
2 Comments
More posts from themightymo.com
Reorganize Your WordPress 2.7 Admin Panel with Fluency 2.0
Yesterday I discovered the new Fluency 2.0 plugin for WordPress 2.7. It does a fantastic job of cleaning up the admin interface and makes the back-end WordPress experience much better for users.
We Now Offer Search Engine Optimization!
We have just teamed up with one of the web’s finest search engine optimization gurus, Greg Cryns, and will now be offering his services to you, our customers! For you, our customer, this means that we now have added expertise in driving targeted traffic to your website. Greg has over a decade worth of search…
Adobe CS4 Flash MN Presentation at FlashMN
Below are my notes from tonight’s FlashMN meeting starring Dustin Tauer. The presentation focused on the new features of Adobe Creative Suite 4. Illustrator CS4 Can create multiple art boards. So, if I wanted to use artwork for the front and back of t-shirt, I would create 2 art boards (up to 100). There are…
Hi,
Thank you for this issue. It’s work fine for me on index.php but not for the archives pages: tags, search, category…
Do you have a solution?
Many thanks in advance,
Dem.
I can’t think of any reason why it wouldn’t work on categories… Please let me know if you figure this out!