On this site, I feel that it is important to only show posts on the Blog home page that are relevant to the site. My Blog entries, however, are rarely related to scrapbooking. Thus, I wanted to find a way to filter the blog posts on the home page to specific categories - namely, excluding mine.
After sifting through the spaghetti archives in both the Support Forums and Codex at Wordpress.org, I found there were quite a few different ways other people have tried to do this. Many of the suggestions didn’t quite work; and some didn’t seem like “good” ways to go about it. First of all, I didn’t want to install a plug-in for something so trivial. Secondly, I wanted to add this feature in the Theme Template that I created rather than modifying code of WordPress files. Finally, I created some test code to try out on my server at home, so that I could see what was going on behind the scenes. After doing so, I found the following code to work well; and it satisfies my requirements stated above — no plug-in and no editing of wordpress files.
Code:
< ?php if(is_home()) { query_posts('cat=1&showposts=10'); } //Set Category for Blog Home ?>
The above code is in my index.php template file in my theme directory. The code can be anywhere in the file, but it MUST be before “The_Loop”!!! Basically, you’re re-setting the $WP_Query object before The_Loop is run.
Following are some reasons why other code examples to solve this problem don’t work (or didn’t adhere to my requirements).
The most frequently used bit of code I found that doesn’t work is this:
< ?php if(!isset($cat) { then do something - i.e. set the category ; } ?>
This code is checking to see if the $cat variable is set or not; and if it isn’t set then it should do something. Well, from my observations, this variable is always set, even if it is an empty string. So, when you request index.php with no query string - you might think that the $cat variable isn’t set. However, the variable IS set and is equal to an empty string. Thus, the above code inside the IF block never executes.
I replaced that bit of code with:
<?php if($cat == "") { do something; ?>
This checks to see if the string value of $cat is empty; and if so, do something. However, this code ‘breaks’ on other pages. So, not a good solution either.
Another bit of code I found said that if the $cat isn’t what you want it to be, you could change it by doing this:
$cat = '1';
Well sure, you can change the value in the $cat variable like that, but it does you no good once Query has already been created. If you want to change the query used in The_Loop, you have to use ‘query_posts()’ like this:
query_posts('cat=1');
Then, when The_Loop executes, it will use the new query information that you just set using ‘query_posts()’. There are many variables that can be set using query_posts(), but that is beyond the scope of this discussion. Check the Wordpress Codex for more information regarding query_posts().
Finally, the best way to change the information or layout of your Blog’s homepage is to use the is_home() function tag. This tag is a wrapper for a variable in the $WP_Query object. Upon looking at the code of the Class Object, it can be determined that the is_home() variable is changed when a new query is run and that it is only TRUE when all the other possible page types are FALSE. i.e. the page is NOT an ‘archive’ or ’single post’ or ‘ page’ or ’search’ or ‘feed’ or ‘trackback’ or ‘404′ or ‘admin’ or ‘comments-popup’. Thus, whenever you change the Query, you must realize that the is_home value might change as well.
I hope that this information is useful. Please keep in mind that everything that I have discussed here is regarding WordPress 1.5.2 only! I have no idea how these snippets of code work in earlier (and/or future) versions.