Excluding Posts and Cheap generic online viagra 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:

  1. A special category would be used to differentiate one blog’s posts from the other.
  2. Posts tagged with this special category would not show up on the index page of the blog.
  3. Posts tagged with the special category would have their own dedicated page which acted exactly like posts on the index page.
  4. And there would be an alert on the home page to let users know when the Buy generic online viagra 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 Buy xenical online 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 Buy levitra online 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 Buy proscar online 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 Viagra online uk 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. :)

Image used for Off-Topic notifications

If you have any questions, do not hesitate to leave a comment or contact me!

Sincerely,
The Closet Entrepreneur

» This entry was filed under General and tagged with:

24 Comments

  1. 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.

  2. TOMAS

    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.

    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?

  3. 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 :)

  4. TOMAS

    Great to hear that you got everything figured out!

  5. 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

  6. 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.

  7. Hi Mark,

    Very helpful information.

    Really enjoyed reading the tips. Keep up the good work.

  8. Hey. Your tutorial is very helpful indeed. However, I landed here searching not for what you did, but for something close :

    I want the index of my WP to display not a static page, nor posts from specific category, and not to exclude specific cats from the index. I want the index to display the latest post (or x posts) from the categories that I select. My movies blog contains posts in various film-related fields: movie reviews, articles about cinema and movies, funny clips, actors news, etc. I dont want all that be mixed up in the same page altogether. I want them in the same page, but in a more organized way. So I was planning to make the index show latest 1 or two or 3 posts from each category (the fields), where I separate them with graphical indicators saying “latest in reviews”, “latest in actors”, etc.
    This could be done somehow.

    Thanx for taking the time to read this. And an extra BIG THANX if you help me :)

  9. @Mouad – I’m wondering if your best bet is to try grabbing the rss feed from the categories that you’re interested in—each category has its own feed that should look like this:

    http://yourblogsname.com/category/nameofyourcategory/feed/

    …for example, you can see the feed for my blog posts filed under General at:

    http://theclosetentrepreneur.com/category/general/feed

    Your next step would be to use an RSS plugin to parse and format the feeds on your index page. SimplePie seems to be one of the more popular feed managers and many RSS plugins for WordPress have been created with it. Your other option is to use WordPress’ RSS codex (e.g., fetch_rss) but I’m not sure how multiple feeds are handled on the same page.

    Regardless, I hope this info helps! :)

  10. I used the trick of !in_category(’80′) to skip all post in that category.
    The problem is my “showposts” is consumed even if the post was skipped. So, for the excerpts section at the bottom of my page, there are not 12 neatly grid-aligned posts, but 11, or less.

    How can I increment the showposts++ value as the cat posts are skipped?

    PS: I like your website’s design.

  11. @sergio – To be honest with you, it’s been a while since I’ve peeked into this WordPress code so I really can’t be of much help at the moment. I’m hoping that someone reading this post could lend some help, and there’s always the WordPress gurus on WordPress.org.

    Also thanks for the kudos on the site; I’m glad you like the design!

    :)

  12. I’ve been looking for a solution like this for some time. I’ve been able to add the latest posts from a specific category, but the next and prev buttons do not work.

    In the example of if (in_category(’27′) && is_home() ) continue;

    How can I exclude two categories in this statement?

    Thanks.

  13. I think I found the solution

    if (in_category(’1′) && is_home() ) continue;
    if (in_category(’2′) && is_home() ) continue;

    Thanks for the helpful article. I really appreciate it.

  14. @Adriana – Glad to see you found a solution and thank you so much for sharing it! Nice blog by the way! :)

  15. John

    Not normally one to post up sites providing code snippets that often, but I just wanted to say a huge thanks for this – constructing a similar site to the one in your example and was being driven nuts by blank pages and all kinds of other errors until I tried your function – thanks for saving my sanity!

  16. Ted

    I cant get the posts tagged with my specific category to their own dedicated page. Can anyone be more specific?

    “Posts tagged with the special category will have their own dedicated page which acts exactly like the index page.”

  17. I can’t get the permalinks to work in my category-8.php file. I want to lead them to their own separate dedicated posting page. But all my permalinks leads to the index.php.

    Any ideas?

  18. Hello thanks for your post now it clear to me on how exclude my post on my page

  19. Tim

    THANK YOU! Your explanation was exactly what I needed. Helped me do exactly what I needed to do.

  20. angela

    thank you so much!
    exactly what i needed!!

Leave a Comment

*
Please type the answer to the math equation. Click the pic to hear the numbers.
Click to hear an audio file of the anti-spam equation