The title of this blog post is really long, ain’t it? But it needed to be that long to explain the scenario correctly.
I use /%category%/%postname%/ permalink for my blog posts. At the same time, I have gotten rid of the default category base “category” by using the Top Level Categories plugin. What Top Level Categories plugin does is, it makes the category page URLs look like your-domain-name/category-name instead of the default your-domain-name/category/category-name, the latter has an extra “category” in the URL. I did this to create a perfect directory silo structure at my blog. I will talk in detail about siloing in my coming posts. A directory silo structure requires that each directory in the URL displays a valid page. I had to get rid of the default category base using the Top Level Categories plugin because SomeUsefulinfo.com/category/ would not return a valid page and give me a 404 error message in WordPress. That’s understandable because /category would itself mean nothing without the actual category-name added to the URL after “/category”, i.e SomeUsefulinfo.com/category/category-name (this one was working fine) and also who wants to add an extra “/category” in the category page URLs just to make the URL long?
Things were fine but after I added subcategories to my blog, everything screwed up. I cannot understand this thing with WordPress because before adding the subcategories, all of my category pages were reachable but after adding the subcategories, although categories and subcategories are expected to behave in similar ways, subcategories pages started giving me 404 errors. After digging deep, I found out that for subcategories pages to work (not throw 404 error), the category base (either your own or the default “category”) needed to be present in the URL. So the thing that was happening was:
SomeUsefulinfo.com/some-category-name/ was working fine
BUT
SomeUsefulinfo.com/some-category-name/some-sub-category-name was not working and gave 404 errors.
However, both would work if I added the category base “/category”. That means,
SomeUsefulinfo.com/category/some-category-name/ and
SomeUsefulinfo.com/category/some-category-name/some-sub-category-name would both work.
So I was left with two options: either to remove subcategories and work a way out by adding subcategories as categories or stop doing siloing. At last, I found a solution that let me use subcategories as well as silo my blog. If you want to to the same thing, the solution is not that hard. You don’t need any programming skills, you only need to know how to copy and paste codes.
Here’s the step by step guide that I’ve created for getting rid of the 404 errors thrown by sub-category pages in WordPress while using /%category%/%postname%/ permalink and disabling the default category base “/category” using the Top Level Categories plugin or editing your WP codes. The guide below assumes that you have already changed your permalink structure to /%category%/%postname%/ and installed the Top Level Categories plugin.
1. Add subcategories to the parent category by logging onto WP and going to Manage>Categories.
Now, after you add posts into your subcategories, your subcategories will start appearing under your parent categories on your blog’s sidebar or wherever you have kept them. Now clicking on those subcategories’ links will give you 404 error pages. (because the URL is devoid of the category base “category”, which is what we want, thanks to the Top Level Categories plugin, but we need to fix the 404 errors).
2. From Manage>Categories, hover your mouse pointer over the link of the parent category to get its category-id. As you hover, check your status bar of your browser, you will see something like:
your-domain-name/wp-admin/categories.php?action=edit&cat_ID=XX
That XX is the category-id of your parent category. Note it down or remember it. However, noting it down is the better option.
3. Go to Write>Page. Create a static page with the same slug as your parent category and publish this page. To do this, you need to edit the slug that WP generates after you enter the title of your page, such that it matches with the slug of your parent category. By default, if you don’t specify a slug while creating categories, slugs for categories are generated automatically by WordPress. By default it looks exactly similar to your category name with hyphens replacing the spaces. For example if you create a subcategory with the name “404 error pages”, the default category slug for this category would be “404-error-pages” and hence the slug of the page should also be “404-error-pages”. After you have done this, when you click the link to the parent category from the blog’s sidebar or wherever it is present, it should take you to this page and not the category template that WP generates that lists all the posts posted under the subcategories of this parent category. WP will take you to the page that you crated for the parent category and not to the one that it generates itself (although the slugs are the same) because the preference order for user created pages is higher than for the templates that WP generates by itself.
4. To silo a WordPress blog, on the category page for the parent category, you should only keep the links to the subcategories of that category and not the links and excerpts of the posts posted under the subcategories of this parent category. So what do you do? You need to install the Exec-PHP plugin (chances are, you already have this plugin installed). This plugin lets you execute <?php ?> code in your posts, pages and text widgets. Why do we need this plugin? Because it’s easier to list the subcategories of a category using PHP codes. In fact, we can also do this without PHP, but, answer me, what’s better, a one line PHP code or a 10-20 lines static HTML code plus the pain of changing the HTML code every time you add new subcategories under the parent category? Certainly, the PHP option is better.
5. Go to Manage>Pages and then click on the page that you created on step 3.
6. Go to HTML view of the page and copy and paste the following code:
1 | <?php wp_list_cats('sort_column=id&use_desc_for_title=0&optioncount=1&child_of=XX'); ?> |
where, XX is the category-id of the parent category that you jotted down or memorized earlier. If you had tried to memorize the category-id earlier and don’t remember it now, you need to get back to step 2. I told you earlier that noting it down was the better option. You also need to stop thinking you have got good memorizing power and from next time, be ready with a paper and a pen or at least use “notepad”.
7. Now, this step is going to be painful if you have added a lot of subcategories. You need to create static pages for all the subcategories that you have added and specify the page that you created at step 3 as the parent of all the subcategories pages that you create now. To do this, go to Write>Page. Edit the slug that WP generates after you enter the title of your page such that it matches with the slug of the first subcategory. And also don’t forget to specify the page that you created at step 3 as the parent of this page. While you create the pages for each subcategories, you need to add php codes to the pages so as to display all the posts that are inside that particular subcategory. See, we need the Exec-PHP plugin here yet again.
Go to HTML view of the page and copy and paste the following code:
1 | <?php query_posts('category_name=xxxxxxxx'); ?> |
where, xxxxxxxx is the name of the subcategory for which you are creating a page. xxxxxxxx should be exactly similar to the slug that you have specified while creating this subcategory earlier. By default it looks exactly similar to your subcategory name with hyphens replacing the spaces. For example if you have created a subcategory with the name “sub category 1″, you need to add the following code:
1 | <?php query_posts('category_name=sub-category-1'); ?> |
We need to copy and paste some more codes below this code. Now if you have an offline backup of the files of the theme that you are using, open archive.php file from your theme folder in your favorite text editor. We need to copy codes from this file and paste it on the page we just created. If you don’t have an offline copy, you can get it by logging onto your WP Admin and going to Design>Theme Editor and selecting archive.php file from the long menu on the right. I had to paste the following code from the archive.php file into my page to get the job done:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <div class="post-top"> <div class="post-title"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php if ( function_exists('the_title_attribute')) the_title_attribute(); else the_title(); ?>"><?php the_title(); ?></a></h2> <div class="tags"> <table cellspacing="0" cellpadding="0" width="100%"> <tr> <td align="left"> </td> <td align="right"> <h4> Posted by <b> <?php the_author() ?> </b> on <b> <?php the_time('F d, ') ?> </b> <?php the_time('Y') ?> </h4> </td> </tr> </table> </div> </div> </div> <div class="entry clear"> <?php the_excerpt(''); ?> </div> <div class="postmetadata"> <div class="alignleft"> <b>Categories:</b> <?php the_category(', ') ?> </div> <div class="alignright"><?php comments_popup_link('No Comments', '<span>(1)</span> Comment', '<span>(%)</span> Comments'); ?> <a class="readmore" href="<?php the_permalink() ?>" >Read more</a><?php edit_post_link('Edit', ' ', ''); ?></div> </div> </div> <?php endwhile; ?> |
Search for similar code on your archive.php file of your theme. Copying and pasting the exact similar code from above will screw the design of your blog unless you are using the same theme as mine, so remember to copy and paste the code from the archive.php file of your theme and not the above code. By the way, I am using the BlueMin theme. What this code does is, it makes the user generated subcategories page look exactly similar to the ones generated by WP (had they been working correctly
)
Now you need to add one more line below the above code:
1 | <?php query_posts('category_name=abracadabra'); ?> |
The above line prevents WordPress engine from looping through unnecessary posts. I chose the term “abracadabra” in the above code randomly and it can be replaced by anything other than categories or subcategories slugs of your blog.
Hit publish and you are done. The subcategory pages should work fine now and stop throwing 404 errors.
8. Repeat Step 7 for all the subcategories that you have created.
If you come across any problems, drop a comment below and I will try my best to help you out.
Happy Blogging with WordPress.


Hello,
Thanks so much for this.
I have one problem. On the page that displays the subcategory list containing this:
It is also showing all the posts for this slug (sub-category-1) which is good but one of the posts IS the page that was created. I don’t want that page to show in the list of the posts associated with the subcategory sub-category-1.
Can you help? Thanks.
Reply to this comment
Hi,
I am using wp 2.7.1.
/%category%/%postname%/ permalink and disabling the default category base “category”
Installed Top Level Categories
From plugin homepage:
Known Issues / Bugs
1. This plugin will not work if you have a permalink structure like %postname or %category%/%postname% — there is currently no workaround
Not sure if it applies to what is being done here.
Thanks in advance for any guidance on this.
Reply to this comment
The WP-category-no-base plugin solves this problem! It does the same thing as Top Level Categories, but without this bug. It’s working well for me on WP 2.7, check it out here:
http://wordpresssupplies.com/wordpress-plugins/no-category-base/
Reply to this comment
Thanks for sharing this plugin. The plugin really does the intended work. I have already made use of the WP-category-no-base plugin upon your recommendation here at my blog and have deleted all the pages I have created before to get through the 404 error on sub categories pages. Really, the plugin saves a lot of time and I recommend this plugin to everyone who want to remove the category base from their permalink structure.
Reply to this comment
The ‘no category base’ plugin works very well. Thank you for pointing it out Thaddeusmt and Ghatozhat for providing the earlier hack.
Reply to this comment
Thanks for posting Ghatozkat, thi swas driving me nuts on a few of my sites. IN the end, I also ended up replacing top level cats with the “wp no category base” plugin. Works perfectly!
Reply to this comment