Announcement

Collapse
No announcement yet.

How to Exclude Posts of Certain Categories to showup on the Front Page in Wordpress

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Exclude Posts of Certain Categories to showup on the Front Page in Wordpress

    Today, for example, I wanted the “Quote of the Day” on WhoTube to be only in the sidebar, not in the main column of content. In my personal blog, I want to exclude my Twitter archives from the front page. And happily, this is a very easy thing to do; you don’t even need a plugin, just one little theme edit.

    Firstly, you need to find the number of the category you want to exclude:

    go into Posts > Categories
    click on the relevent category name
    the URL of the next page should end &cat_ID=xx, where xx is a number. Make a note of it.

    Next, go into Appearance > Editor, and open functions.php. Add the following function:
    Code:
    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('cat', '-xx');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');
    where xx is the number of your category. Don’t forget the minus sign in front of it! Save it, and you’re done.

    Excluding multiple categories
    is easy. Just add the extra category numbers, making sure you have a minus sign in front of each one:
    Code:
    $query->set('cat', '-1 -2 -3 -4 -5');
    Excluding categories from archive pages
    This is also easy. The second line of the code above says “if the query’s being run on the home page”; just change it to say “if the query’s being run on an archive page”:
    Code:
    if ( $query->is_archive ) {
    or if you want home page and archive pages to exclude the category:
    Code:
    if ( $query->is_home || $query->is_archive) {
    Similar Threads
Working...
X