Need to Block & Exclude Categories in Wordpress? Here is the Solution
Sometimes we need to create a special category to feature posts or other information in the homepage or somewhere else, but don’t want it to appear in the template under the category list or get indexed by Search Engines because of potential duplicate content issues.
One alternative is to block that category from being indexed. The other option is to completely remove it from the template to limit any possibilities to be found and indexed. I will explore both techniques in this post.
Be aware that I will be using the ‘featured’ category with ID number 52 for example purposes.
Blocking Categories with Robots.txt
The easiest way to block the featured category from being indexed is by blocking the Search Engines using the disallow command in robots.txt:
User-agent: Googlebot Disallow: /category/featured/ User-agent: * Disallow: /category/featured/
Even though the category will be blocked, it will still appear in SERP if there are links pointing to it anywhere on the web. Make sure there are no external links to this category, or if there are, they should use the nofollow attribute value of the rel attribute in the <a> tag.
Nofollow and Noindex in the Robots Meta Tag
The best way to avoid indexing a page in the search results is by adding the noindex attribute in the Robots Meta tag in the header section of a page. Use this code in header.php between <head> and </head>:
<meta name="robots" content="noindex,follow" />
Keep in mind that even with the noindex attribute in the Robots Meta tag, links will be followed and PageRank (PR) will be distributed to other pages. If you don’t want links to be followed, then replace the ‘follow‘ attribute by ‘nofollow‘. The nofollow attribute will prevent the page from passing PR juice.
In addition to the changes in the Robots Meta tag, make sure that all internal links pointing to the featured category have the nofollow attribute.
The nofollow attribute in the Robots Meta tag applies to the entire page, and the nofollow attribute on the link (<a> tag) applies only to that link.
The Navigation Menu
If the featured category shows up in the navigation menu, you will have to remove it by adding &exclude=cat_ID to <?php wp_list_categories(); ?>. The navigation menu is normally located in header.php.
cat_ID is the number assigned to the featured category. This can be found by hovering the mouse over the category name under the manage categories section. Information about the category will appear at the left bottom corner and will look similar to this:
http://www.yoursite.com/wp-admin/post.php?action=edit&post=52
Category List in the Sidebar
If you are using a widget to show categories in the sidebar, then you will have to make changes to the widget according to the developer’s suggestions.
But if the category list was hard coded in the template, most likely you will have to edit sidebar.php to exclude the featured category by adding &exclude=cat_ID to <?php wp_list_categories(); ?>.
This an example of the code that shows all categories in the sidebar in a nested list, sorts them by name, and excludes category 52:
<?php wp_list_categories('sort_column=name&title_li=&exclude=52'); ?>
The process is very similar for the sitemap.
Blog’s Sitemap
If you have a sitemap in your blog, it is very likely that is named sitemap.php. You will have to remove the featured category from the category list by adding &exclude=cat_ID to<?php wp_list_categories(); ?>.
This an example of the code that lists all categories in the sitemap, sorts them by name and excludes category 52:
<?php wp_list_cats('sort_column=name&exclude=52'); ?>
Blog’s Main Page
This could be either your homepage or a page located in a folder such as /blog/. As you can see in our Multilingual SEO blog page, each post shows the category to which was assigned, right next to ‘Filed under’ at the end of the post.
If you don’t make any changes to the code in this section, the featured category will appear next to other categories to which were also assigned.
To remove the featured category simply replace <?php the_category(’, ‘) ?> with this code:
<?php foreach((get_the_category()) as $cat) { if (!($cat->cat_ID=='52')) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', '; } ?>
Make sure to change cat_ID==’52′ for the number of the category you are removing.
Category Pages
Posts assigned to one or more categories are contained in one or several pages. At the end of the post, the categories to which they were assigned appear next to ‘Filed under’. To remove the featured category replace <?php the_category(’, ‘) ?> in archive.php with the following code:
<?php foreach((get_the_category()) as $cat) { if (!($cat->cat_ID=='52')) echo '<a href="' . get_bloginfo('url') . '/category/' . $cat->category_nicename . '/">'. $cat->cat_name . '</a>' . ', '; } ?>
Make sure to change cat_ID==’52′ for the number of the category you are removing.
In case you deal with pagination make an effort to use the nofollow attribute for links to the second, third, or fourth pages as well as noindex attribute in the Robots Meta tag in those same pages. Enjoy it ![]()
Popularity of this page: 55%
If you found this information interesting, please add us to your social network or link to us using the code below
Related information:




















Comments
Got something to say?