I’m using WordPress page templates to pull posts with certain categories into their own page. My problem is that they are also showing on the home page. I know how to hide categories from the home page entirely, but I’d like a toggle so that if it’s checked, it will post to home page and sub-page and if it’s not checked, it will display only on the sub-page. Does that make sense?
This code is on the “Awards” template and pulls in posts containing the category slug “awards”.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'awards', // Change these category SLUGS to suit your use.
'paged' => $paged
);
query_posts($args);?>
The “awards” category is then blocked from the home page using a query_posts tag:
<?php if ( is_home() ){
query_posts( 'cat=-5' ); //-5 is the category id for 'awards'
}?>
I tried to get around this by also tagging the post with a category that isn’t blocked from the home page, but it apparently doesn’t care that it’s tagged with something else.
So my conundrum is that I would like a simple option (hah) that allows me to pick and choose if it belongs on the home page or not, while still having them all show on this other page.
Well.. in the middle of writing this, I was inspired and sure enough I found the simple solution. ‘Uncategorized’ is the default category and has the id of 1. On the index page, I simply added the ‘query_posts( ‘cat=1′ );’ underneath the bit of code that blocks the other categories. This way, I can tag it for the category I want on one of the sub pages, but also for ones that I want to show on the homepage. Here is the code as it is on my index.php:
So if I want it on the home page AND the subpage, I’ll categorize it as ‘awards’ AND ‘Uncategorized’ (or whatever you choose to be the homepage category). If I want it only on the subpage, I just choose that category. This works because the ‘query_posts( ‘cat=1′ );’ is written below the ‘query_posts( ‘cat=-5′ );’ part, so it’s happening after the block which tells the server to display it after all.