Excluding Posts and Category Specific Pages in Wordpress
Updated 11/14/06: Corrected PHP code.
I created 2 WordPress blogs to keep my “business” and “pleasure” in separate places, yet just trying to keep up with one blog has been difficult enough. Plus, visitors have to visit 2 blogs instead of one, so there’s an unnecessary inconvenience on the user’s side too. I began looking into ways of incorporating and combining my two blogs into one and stumbled across various methods of excluding posts from the home page and creating category specific pages in WordPress.
I had the following goals in mind when I started:
- A special category would be used to differentiate one blog’s posts from the other.
- Posts tagged with this special category would not show up on the index page of the blog.
- Posts tagged with the special category would have their own dedicated page which acted exactly like posts on the index page.
- And there would be an alert on the home page to let users know when the special category page had been recently updated.
It all sounds like a lot of work, but things aren’t terribly difficult after becoming familiar with WordPress’s various features.
Item #1: “A special category will be used to differentiate one blog’s posts from the other.”
Using a special category to differentiate one blog’s posts from another is simple - just use Categories. You can create a category by going to “Manage” and then “Categories” in your WordPress Admin page. While you’re there, you will also be able to find the category ID for your newly created category. Take note of your category ID because you will need it later. In my case, I named my category “Off-Topic” and it was assigned a category ID of 27. I’ll be using a category ID of 27 in this example, yet your category ID will likely be different.
Item #2: “Posts tagged with this special category will not show up on the index page of the blog.”
Excluding posts tagged with a certain category from the home page can be done in a variety of ways, yet each method has its own set of pros and cons. One method of doing this is by using the query_posts template tag and entering the following code in the index.php file just before the WordPress Loop, also known as “the Loop“.
<?php
if (is_home()) {
query_posts("cat=-27");}
//Exclude posts in category 27 from the home page
?>
The only problem with this method is that posts must only belong to the category being specified and you can only exclude one category. If a post belongs to another category, then it will appear on the home page. Since I wanted the flexibility of assigning posts to multiple categories, this method wasn’t used. Although one nice thing about this method is that it will show the correct number of posts on the home page that you have specified in your WordPress options regardless of the number of posts that are excluded; you’ll see why this matters in a moment.
The method I settled on was using an “if” statement in “the Loop” to exclude category 27 from the home page. The code looks something like this:
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
//This is the start of "the Loop"
if (in_category('27') && is_home() ) continue;
//If the post is assigned to category 27 and the current page is the home page, then skip the post and continue. Else, continue as normal.
?>
//Here goes all the code that outputs your posts
<?php endwhile; ?>
//Here goes the code that directs users to past and previous post entries
<?php else: ?>
//This is where the code that tells the users that no matching posts were found resides
<?php endif; ?>
The nice thing about this method is that posts assigned to multiple categories will be skipped as long as they have been assigned to category 27. The drawback is that WordPress does not account for the post or posts that have been skipped, so the number of posts to be displayed on the home page will be incorrect if a post is skipped. For example, if you have specified that you want 10 posts to be displayed, and 6 of those 10 posts have been assigned to category 27, then only 4 posts will be displayed on your home page. This shouldn’t be a problem if you don’t care about the correct number of posts being displayed on your home page, it only becomes a problem if more posts are excluded than you are showing. In this situation, the user will be greeted with a post-less home page. In my case, I’ve asked WordPress to display 3 posts on the home page which allows me to have 2 of the most current posts be assigned to the Off-Topic category without getting into trouble.
Item #3: “Posts tagged with the special category will have their own dedicated page which acts exactly like the index page.”
Having posts with category 27 appearing on their own page and behaving exactly like the index page wasn’t too difficult. I simply made a copy of my original (not modified) index.php file and renamed it category-27.php and uploaded the file to my wp-content\themes\your_theme directory. In a nutshell, I used the code from my index.php page to create a category template for category 27. You can do this for any category by creating a page called category-n.php (where n is the category ID) and uploading it to your theme’s root directory. WordPress knows exactly what do to when it encounters a file like this. I won’t go into much detail about it since the WordPress Codex page describes the process of making a category template much better than I can. After creating the category template, I hard-coded a link to the category in my navigation bar by using the following URL:
http://theclosetentrepreneur.com/?cat=27
Item #4: “There will be an alert on the home page to let users know when the special category page had been recently updated.”
Item #4 in my list of goals was implemented by using a bit of code from Shaun Andrew’s Wordpress Sticky Post tutorial. I slightly modified his code to display an image in the sidebar that lets users know that an Off-Topic post was recently posted; Shaun’s original code displays a “sticky post” instead. The code resides in my sidebar.php file and looks like this:
<?php
$my_query = new WP_Query('cat=27&showposts=1');
// Initialize a new query that grabs the latest post assigned to category 27
while ($my_query->have_posts()) : $my_query->the_post();
// "the Loop" makes details of the off topic post available
$goAwayDate = time() - (60 * 60 * 24 * 2);
// Create a variable that calculates 2 days in Unix Epoch time
$postDate = get_the_time('U');
// Get the time of the latest category 27 post in Unix Epoch time
if ($postDate <$goAwayDate) {}
// If the date of the post is more than 2 days old, do nothing. Else display the image.
else { ?>
// This is where the HTML and CSS code resides that displays an image in the sidebar to let users know that an Off-Topic post has been posted
<?php } endwhile; ?>
Again, this may seem a little overwhelming if you’re not familiar with PHP or WordPress, but a little reading and research will help out tremendously. As is described in Shaun’s write-up, the $goAwayDate is just the current time minus 2 days which is calculated in seconds:
60seconds x 60 minutes x 24 hours x 2 days
In the end, what I ended up with is a separate Off-Topic page with all the functionality of my index page whose posts are not shown on my home page. So in essence, a second blog added to my existing blog. In addition, users are greeted with the image below in my sidebar for 2 days from the time a new Off-Topic post is created. The “Hot - Now” was inspired by Krispy Kreme.

If you have any questions, do not hesitate to leave a comment or contact me!
Sincerely,
The Closet Entrepreneur
8 Comments
theeAdversary
Very help article. One question though, I have separate category pages working, it shows the ‘comment’ link but clicking on it only brings up a blank page. Have I messed something up? Cheers.
Nov 30th, 2006
TOMAS
I was checking out your site and the comments seem to be showing up just fine on the homepage. Was this happening on your category pages only or on the entire site?
Nov 30th, 2006
theeAdversary
Ah thanks for looking. Yeah it was specifically on ‘Raymie’s PhotoBlog’. I have it working now. I didn’t user your code correctly. I had an if (in_category(’9′)) around everything in the loop instead of the if/continue that you had. I have now changed it and it works fine. Thats what I get for glancing at your code and jumping in to mine. Thank you very much
Nov 30th, 2006
TOMAS
Great to hear that you got everything figured out!
Nov 30th, 2006
Mark
This post is EXTREMELY helpful and I am going to try and incorporate it into my “daily-photo” page. Currently, I update the picture and then delete the old one everyday on the same page but this, of course, is not how I want it. With your solution it seems as though I can now archive all of my daily photos.
Thanks again,
Mark
http://www.mytropicalescape.com
Jul 11th, 2007
TOMAS
Hi Mark,
I’m glad you were able to make use of the info, and you might also want to take a look at Rich’s Category Visibility Plugin to see if it will save you some work.
Jul 11th, 2007
Work at home guide
Hi Mark,
Very helpful information.
Really enjoyed reading the tips. Keep up the good work.
Dec 2nd, 2007
Leave a Comment