How to Exclude a Category From Your WordPress Home Page

3 Comments

If you have a WordPress site with a list of posts for your home page, it is easy to exclude a category so it doesn’t appear in the list. You might want to do this if you have a category that appears as a page in it’s own right, such as a client area on the Andrew Olney Wedding Photography site for example. (The client weddings are in the category of Weddings and have their own button on the site navigation.)

It is fairly easy to exclude a category from your home page with 4 lines of code, or 5 if you include the comment!

Open up your loop.php (or page.php depending on your theme) file and look for the line that starts the loop:

 

<?php while ( have_posts() ) : the_post(); ?>

Just before that, you want to add this:

<!– exclude the weddings category from the home page –>
<?php if ( is_home() ) {
query_posts($query_string . ‘&cat=-1’);
}
?>


In this example, the number 1 refers to the category we want to exclude. To find the category number you need to go to the Categories page in the admin panel and hover over the category name. You’ll see the number in the status bar at the bottom of the browser window.

Save your loop.php (page.php) file and that’s it.

A side note for people using the twentyten or twentyeleven themes who want to display a category on it’s own page; these themes use the_excerpt() to display the listed content. If you have links in your entry that are not showing or working as hyperlinks, look for this section of code in your loop.php file:

 

<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>

 

Change the_excerpt() forĀ the_content() and your links will work again.

WordPress , , , , ,


3 Comments

  1. Iziwebavenir

    Hello

    a new plugin to do what you speak about

    wordpress.org/extend/plugins/wp-exclude-from-homepage/

    best regards

    thomas

  2. Stuart

    Thanks for that Thomas.

  3. blogging tips

    I add this but first time i get error then i apply 2nd time i was successfully add and work 100%. Thanks for share nice article

It's good to talk!

Your email address will not be published. Required fields are marked *

Please answer before submitting form * Time limit is exhausted. Please reload CAPTCHA.