9l7g52vg63xx8t3h
Ok, this has bothered me for a long time and now i have finally found a solution for it thanks to Kafkaesqui moderator on WordPress Support. As the title says when you are using a query_posts() to exclude, include categories or what ever you want pagination dosen't work it shows same posts on every page (This is for all pages that use query_posts not just index, so it's for templates etc...)
Where's the problem? query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is.
How to fix it? To get proper pagination with query_posts() we need to recreate it through the 'paged' parameter or query. Best way to do this is to ask WordPress for the "page" we happen to be on, and use that as our 'paged' value. There's the code for it
<?php if (have_posts()) : ?> <?php query_posts("category_name=somecat"); ?> <?php while (have_posts()) : the_post(); ?> replace with <?php if (have_posts()) : ?> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=somecat&paged=$paged"); ?> <?php while (have_posts()) : the_post(); ?>
And that's it! The $paged = line above uses what's called a ternary operator to test whether the 'paged' query variable is available. If it is, $paged receives its value. If it isn't, we assume we're on the first page and assign it '1'. Then we tell query_posts() what page we're on with the addition of &paged=$paged.
9l7g52vg63xx8t3h
Thanks for the information!
This is a problem every Wordpress Theme Developper runs into some time.
Thanks a ton for the great information. I’ve been wrestling with this problem for some time now. Cheers.
That is for WP_Query though… Not sure if it’d work in query_posts()
A quicker fix I find is to simply add $query_string to the start of the query:
query($query_string.’cat=-1&offset=3′);
This is the best solution for this that I’ve found in the past 4 hours. Nothing on the Wordpress forum I found comes close to working as well as this does. Thank You!
Thanks mate!
I had this problem before.
Two days ago I had this same problem and resolved reading this post on WP forum: http://wordpress.org/support/topic/152451?replies=13#post-679318
But your solution seems more elegant. ![]()
I’ll try it!
Awesome dude
ohoho fantastic stuff! Thanks a lot man!
on November 29, 2008
Thanks for that one, was wondering why I couln’t get my showcase to paginate over correctly..
Will showcase your site
http://wpcult.com/showcase/stylizedweb