Select Page

Using the WordPress platform, you must have had a situation where the WordPress does something that you do not like and you would like to prevent that. The problems may vary from loading the inappropriate homepage to the wrong redirection of URLs. One of the problems that might be bad for users, especially when it comes to staying within the good SEO limits, is showing the child categories or sub categories. While there are some plugins that eliminate this problem, the bigger problem is that it does not work every time. So, how can you solve this problem efficiently?

The problem of subcategories

Let’s say that you have a category “weather” and inside this, you have two subcategories: “world” and “the US”. When a user visits your site and presses on the “weather” category, he will see all posts from that category but will also see these two mentioned child categories. Now, this becomes irritating and sometimes bad for your SEO so you will probably want to eliminate this feature and allow only to see the main category.  Even Google can ban your site as it can think that you are duplicating the content by adding the same post in two different categories, so the experienced developers will want to get rid of this problem. Even if you are not that experienced, do not worry as you will be able to solve this problem easily.

Sub CategoriesDo not worry really, the solution is not far away and everyone who has at least a basic understanding of coding can perform this on his own and solve the problem.  Even if you do not have, just follow the instructions that are described below. So, how to solve this problem, you might ask? Should you use plugins? Yes, there are plugins that solve this, but very often they crash and the user still can see the child categories. To avoid plugins, you can use a simple hack that will solve this.

How to solve it without a plugin?

First of all – back up your archive.php and category.php before you do anything! You do not want to lose your posts and archive! Open archive.php and inspect the code. Try to find a line that is similar to this:

“<?php while (have_posts())” : the_post(); ?>”. Once you find this line, you need to add another line, AFTER THIS LINE. Now, add:

“<? if ((is_category()) &amp;&amp; in_category($wp_querry->get_queried_object_id())) {?>”.

The function of this line is to assess whether we see a category and that the post we are looking at is a righteous member of this category. Now, find the line:

“<?php endwhile; ?>” and add the following line before it:

“<?php } ?>”.

You have basically added this small modification that will prevent the WP to show subcategories, but only the parent category. It is required to have some posts in the “weather” category in order to function correctly. So, you did a small modification to your WP theme!