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.
Have some feedback? Leave a comment



Nice… now my gallery page on my new wp-theme works as it should. Alright!
Worked a charm thank you
from a solid starting point. Basic is infact many templates in one using a single stylesheet. By adding/removing small snippets of code from the html, Basic can go from a fluid 3 column web template for a fixed
WICH FILE I EDIT THIS??
NOBODY TOLD THIS =((((
help!!!!
wow amazing i love it ..perfect..
wow amazing i love it ..perfect.
Was going round in circles with this – thanks for the tip. I adapted the code as wanted to display all posts in a blogroll:
In answer to Alberto – you edit this in your page template file. In my case this was page-blog.php
Thank you for this solution!
Pingback: WordPress主题设计笔记 » 虾子酱油学习网页设计
Great solution! Thank you very much!
Awesomenessssssssssssssss.
Excellent , everything works again now!
Thanks
In A, B, C, high levels of vitamins and beta carotene barındıranAfrican Mango, form meets guidelines as well as the effect of contributing to the beauty of your hair and skin. Beta Carotene, to ensure the beauty of the skin, night vision to strengthen the immune system against disorders, a variety of infections contribute to the substance.Those who want to enter the world healthy and permanent form, which is a product of choice for African Mango, now in Turkey. Because people prefer to use a 100% herbal product that helps people to enter the African Mango form a single product.Hii Thanks…
http://www.africanmangosiparis.com/
Those who want to enter the world healthy and permanent form, which is a product of choice for African Mango, now in Turkey. Because people prefer to use a 100% herbal product that helps people to enter the African Mango form a single product.
Thanks, man. That was killing me! $##$A%#$%$$!!!!!
Thanks for codes
Thank you, this worked exactly as I’d hoped.
Just a note for others: This is the correct solution.
Thanks!
hello i tryed to apply your code but didnt worked
im having problems with pagination in a template wordpress file – im not using it as my home page – its just a template page that show some categories.
found a lot of answers in internet but none worked – and also your post is the only i found talking specific about template page
maybe you can help me to find what im doing wrong ..
this is the page im talking about:
http://www.travelcouture.com.br/?page_id=9
my code:
and also:
<?php next_posts_link( __( '← Older posts’, ‘matala’ ) ); ?>
<?php previous_posts_link( __( 'Newer posts →’, ‘matala’ ) ); ?>
thanxx
ops couldnt paste the code.. i think the page link is enough right ? if anyone have some clue im really desperate :S
solved with the code below:
really thanx
damm forgot cant paste the code here
well u can take a look here if needed
http://wordpress.org/support/topic/post-pagination-inside-template-page-set-to-show-specific-category?replies=8#post-2682145
Like a charm. Thank you.
Cheers!
Pingback: Andrew Brennan | WordPress Query_Posts(), Pagination & “More”
Thanks a lot for the great tips
I dont have this:
”
”
I just have this in my index.php:
I dont have this:
‘
‘
I just have this in my index.php:
‘
‘
I dont have this:
I just have this in my index.php:
Holy crap, you are my savior. This worked perfectly with wp simple pagination!
Pingback: Andrew Brennan: WordPress Query_Posts(), Pagination & “More”
You saved my day! Thanks!
Hi Dejan, I spent *hours* trying to solve this problem. Thanks a bundle for posting this solution, it worked perfectly! Cheers, Gizzard
Thanks a lot….
This is amazing…
Thanks! Brilliant solution for my custom query!
Hey, thanks man!
I have classipress 3.1.5 on wordpress 3.5.1
what file.php i have modified?
thanks
Really appreciate, thanks a lot.
I’m amazed, I have to admit. Rarely do I encounter a blog that’s both equally
educative and engaging, and let me tell you, you’ve hit the nail on the head. The problem is something too few folks are speaking intelligently about. I am very happy I found this in my search for something concerning this.
I just like the helpful information you supply to your articles. I will bookmark your blog and check again here regularly. I’m slightly certain I’ll be informed plenty of new stuff right here! Good luck for the next!