Home WordPress How to Hide a Specific Category from the WordPress Home Page

How to Hide a Specific Category from the WordPress Home Page

Speaking of content management system we will never far away from WordPress, a widely-used open source project started by Matt Mullenweg. There are countless websites that built with WordPress, starting from a multi-author site to a personal blog

In WordPress, you can put your articles to a specific category to make it easy for your readers finding articles that really matter to them. By default, your published articles will appear on the WordPress home page

For some reasons, you may want to prevent the articles on a specific category from appearing in the WordPress home page. Sadly, WordPress doesn’t offer any options to do this you you have do it manually by editing your theme. Follow the guide below if you want to hide a specific category from the WordPress home page

Method 1

  • Login to your WordPress dashboard and head to Appearance –> Editor. Choose Main Index Template (index.php) on the right sidebar

main index

  • Find the “<?php if (have_posts()) : ?>” code and add the following code
query_posts('cat=-4, -6, -27, -28&paged='.get_query_var( 'paged' ));
  • See the example bellow if you still get confuse. Press the Update File button once you done

example

  • Please note. -4, -6, -27 and -28 in the code above are the IDs of the WordPress category. To get the category ID please go to Posts –> Categories. Select your desired category and click the Edit link

edit

  • Once the Edit page is opened, go to the address bar of your web browser and you will see a complex URL that contains category ID

cat_id

Method 2

If the first method above is didn’t work, you can try the second method below

  • Login to your WordPress dashboard and head to Appearance –> Editor. Choose Theme Functions (functions.php) on the right sidebar and add the following code.
function hide_category($query)
{
if ( $query->is_home )
{
$query->set('cat', '-25,-112'); //category ID
}
return $query;
}
add_filter('pre_get_posts', 'hide_category');
  • See the example bellow if you still get confuse. Press the Update File button once you done.

2method

.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

MOST-READ THIS WEEK