in WordPress

How To Exclude Category From Index.php In Wordpress

Wordpress Sometimes you have a category in your blog which you want posts for but don't want these posts to be excluded in your main index page. Is blog is a good example of this scenario, I exclude the snippets category from the index.php page because these are normally small posts which are updated regularly. This category posts are only really useful if you are searching for this solution so I don't feel I need to display these on the index page which get in the way of the web development tutorials. For this reason I exclude this category from the index.php page, in Wordpress it is very easily done. Within the Wordpress loop to display posts you will need to use the function.

if (have_posts()) : while (have_posts()) : the_post(); 

The index.php page will automatically get all the posts to populate the have_posts() method. But you can change the data in here by using the query_posts() method. To exclude a category just add the following snippet above the have_posts() method.

if ( is_main_query() && is_home() ) {
    query_posts($query_string . '&cat=-$categoryid_to_exclude');

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

// HTML to display your post

<?php endwhile; ?>

Subscribe To The Weekly Newsletter

Get weekly updates to your email