Skip a category on the main page, but keep the posts per page constant
-
On the main page, I want to skip a certain category so they don’t show.
This code works great from The Loop page in the codex:
<?php if (in_category('10') && is_home() ) continue; ?>
The problem is, let’s say I have two recent posts in category 10. I have my blog set to display five posts per page. If I skip these category 10 posts though, only three posts will show.
How can I skip category 10 posts while keeping the counter loop the same? Or decrement it with something like:
<?php if (in_category('10') && is_home() ) { WP_Query counter--; //<-- Will this work? continue; } ?>
But all this will do is make an infinite loop on the category 10 post to skip and never move past it. Or how about I count the number of category 10 posts in the last five. If it’s greater than 0, then temporarily adjust the posts_per_page to 5+(number of cat 10 posts). Then check the 5+(num_cat_10) for yet more cat_10 posts on another loop iteration. If the result is zero, then I finally have the number of posts_per_page.
The only problem I have with this solution is it’s pretty hacky and I don’t know the function calls to make.
Related: If I were to temporarily modify my posts_per_page number, let’s say to 7, The ‘Recent Posts’ Widget would display all 7 recent posts, including cat10. What is the code for the Recent Posts? I want to roll my own, but I couldn’t find any base code to modify.
- The topic ‘Skip a category on the main page, but keep the posts per page constant’ is closed to new replies.