Pagination Problem When Excluding Certain Categories from Blog Main Page
7th Sep 2009 | Posted by reza | 35 CommentsIn our newest theme, I had to exlcude a category from main loop of that theme. The wordpress codex said that we could use query_post() function to do that
Then they gave an example, if I wanted to exclude a category with ID=3, I could add this line to my index.php file, before main loop lines:
<?php if (is_home()) { query_posts("cat=-3"); } ?>
It worked! The posts from category I wanted to exclude didn’t show up
But, unfortunately, the link to show previous post in my main page didn’t work. If I clicked the link, It didnt show posts that should appear in the second page. Instead, It kept showing posts from the front page
After googling for the answer, I found out that actually, this query_post() function override paged offset.
To get proper pagination, we should recreate it through pagination parameter of query_post
First, we ask wordpress the page that we are on
To do this, we use get_query_var() function:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
That line means if the ‘paged’ query variable is available, $page will receive value of that variable(wich is page number). If not, we assume we are on the first page, and give $page value 1.
and finally, we use $paged as pagination parameter of query_post() :
<?php query_post("cat=-3&paged=$paged"); ?>
This line means…show posts of the page=$paged, except posts from category with ID=3
In short, if you want to exclude category with ID=3 (for example) from your blog main page, place this code before the main loop:
<?php if (is_home()) { $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=-3&paged=$paged"); } ?>
And category 3 will be exclude without any problem in pagination.
credit: Kafkaesqui at wordpress.org
33 Comments to “Pagination Problem When Excluding Certain Categories from Blog Main Page”
Add Comments (+)-
-
I don’t usually reply to posts but I will in this case, great info…I will add a backlink and bookmark your site. Keep up the good work!
-
Amiable dispatch and this fill someone in on helped me alot in my college assignement. Thanks you for your information.
-
Interesting post as for me. I’d like to read a bit more about that theme. Thnx for posting that info.
-
Nice post and this enter helped me alot in my college assignement. Gratefulness you on your information.
-
Thanks a lot for this. I was experiencing this same problem since yesterday when I wanted to exclude a category from the posts page of a client site that im working on and this solution worked great! Subscribing now.
-
Thank you for the code, this saved me a lot of time trying to figure out how to solve this.
-
Good brief and this enter helped me alot in my college assignement. Thank you on your information.
-
Thanks so much man. I was being driven demented looking for a solution to this problem. Glad you could help me out!
-
the syntax for this was off for me, however i found a fix. if anyone has the same problem, try this:
note: i changed it from “if on the homepage” to “if posts exist”
-
Thanks a ton for this, I was running into the same problem when trying to set up my categories. Spent a good deal of time trying to figure this out – and your solution worked perfectly
-
I looked all over the WordPress codex for answers to this, and after lots of code changes, found nothing that solved the problem.
This correctly written code solved the problem straight away. Thanks for publishing this – it’s saved me a real headache – I was ready to rebuild the website in a new template!
Thanks again
David
-
Saved me a lot of googlingthank you so much. cheers.
-
What about if i want to exclude multiple categories?
-
Boy Kafkaesqui, that string is exactly what I was looking for. I, too, used
<?php
if (is_home()) {
query_posts(“cat=-3″);
}
?>and only today realized it didn’t exclude posts past page 1. You’re code work perfectly, down to the category #. Genuinely appreciate your sharing it. Saved me hours of research and testing.
Thanks
vCopia -
I have two languages in my blog, and when i try to go to older posts of one category, wordpress change to default language and not the language that i’m using in the moment
any solutionsthnaks
-
Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My website is in the exact same niche as yours and my visitors would definitely benefit from a lot of the information you present here. Please let me know if this okay with you. Cheers!
-
Yes! This fixed that issue perfectly! Thanks
-
I love you!!!
thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
Thanks a lot!!! Finally!!
-
thanks a lot bud!! you saved my life
-
I’m using the following code (as per your suggestion) on my homepage. The homepage lists the 5 most recent post …. but the OLDER POSTS link takes me to a page that lists the same 5 most recent posts (not 6-10)
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
echo $paged;
query_posts(‘posts_per_page=’.$numposts.’order=DESC&paged=’.$paged);So, oddly enough, I changed paged=’.$paged to paged=’.$page
$page outputted the page you were on. $paged always displayed ’1′ for some reason. So, here is what worked for me…$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
query_posts(‘posts_per_page=’.$numposts.’order=DESC&paged=’.$page); -
like this @vietpn …
-
Thank You Maaaaaaaaaaaaan You Just solved my problem
-
Thanks very much. This post offered a solution that I was unable to find on the WordPress forum.
In case anyone is wondering, the code also works if you need to limit results to one category, rather than exclude posts from one category — i.e. it also works for cat=X, not just cat=-X.
-
When you made these changes, what pages did you make the changes to? Your post never says or indicates which php file(s) you had to modify.
Thanks!
-
Cheers mate…
Trackbacks/Pingbacks
- Tweets that mention Pagination Problem When Excluding Certain Categories from Blog Main Page | DynamicWP -- Topsy.com
- Problema con paginación cuando se excluyen Categorías en Wordpress
THANK YOU SO MUCH!!!!!