Removing the Dreaded Wordpress “Category” Forever!
Who thought it could be so difficult to remove 8 little letters?!?
I recently turned my back on Drupal and installed Wordpress 2.0.5. It isn’t perfect, but it’s much easier to skin (to me anyway).
One of Wordpress’s idiosyncrasies is it appends what it calls a “category base” to each of your posts. So when you want your URL to look like this:
http://www.myblog.com/category_name/post_title.htm
It makes it look like this:
http://www.myblog.com/category/category_name/post_title.htm
Annoying!
Yes, you can change what this word is under options > permalinks area of the admin. But what if you want to get rid of it all together?
After a little digging, I discovered the answer! A simple one line hack that solves my category base woes forever!
On line 36 of the wp-includes/template-functions-category.php file, you’ll see the code:
$catlink = str_replace(’%category%’, $category_nicename, $catlink);
To remove “category” from the URL, simply add this line under it:
$catlink = str_replace(’category/’, ‘’, $catlink);
That’s it! Try it out!
-
UPDATE
I recently created a plugin that takes care of all of this for you. It will come in handy when you go to update WordPress.
-
UPDATE
After some testing, I found that you must have "%category%" in your permalink structure for this to work. Otherwise, your category pages will break.
-
Help Me Improve!
- The only way for my posts to get better is to get your opinion! So help me out, and rate this one! Thanks!
-
Related Posts
-
Category Base Removal Plugin
The Category Base Killer Plugin does exactly what it says. It removes the "category/" text from your WordPress URL string. ...
-
Columbia PA’s “Perfect Settings” Gets a New Face
It seems that everyone I know is either married or is getting married. I myself am on the band wagon being engaged to my fia...
-
More Than I Could Chew
After successfully creating a custom theme for my Wordpress front end, I thought it’d be a neat idea to theme the back end ...
February 23rd, 2007 at 11:12 am
Hi Brian,
Nice hack! Thanks for sharing.
Although your approach works fine, I guess it would be better not to do a string replace on the category link string, but instead, modify the function that actually build permalink structure. A few lines above your modification, you can see a call to get_category_permastruct() that is the function who brings us the annoying “category/” on the link.
Here is my version of the same hack:
On line 371 of the wp-includes/rewrite.php file, you’ll see the code:
$this->category_structure = $this->front . ‘category/’;
Now just remove the “category” from the string, and the code should look like:
$this->category_structure = $this->front . ‘/’;
Done… The same result, but a bit more elegant.
Caio Proiete
www.pdaexpert.net
March 26th, 2007 at 9:22 am
Hi Brian,
Thank You very much for the shared tricks, I had used Caio Proiete’s ‘hack’ before, it worked ! But after I restart my laptop (I tested first in localhost), it said 404 for all pages .. so sad.
Miraculously .. I found this tips..
again thank you, I’ll touch you after my blog is up
May 8th, 2007 at 2:20 pm
UPDATE
I used the method described by Caio Proiete and was having some issues with links inside of the various categories returning a 404 error. I then went back to the original method and it worked.
Strange. I don’t know why it worked and the other way didn’t, but I thought it worth mentioning.
June 19th, 2007 at 8:46 am
My God in heaven! I’m practically bald from pulling my hair out on this one. Thank you for the great solution. Like others, Caio’s approach didn’t work for me, but this works like a charm.
June 28th, 2007 at 11:33 am
Hey Mike. Glad it helped. I too lost a few years of my life on this one.
July 7th, 2007 at 6:13 am
Hi
see this 5 step tutorial that tells you how to remove category base form permalinks. This is only for wordpress 2.0+ the only difference is that it uses category-template.php.
http://bloggerholic.com/wordpress/how-to-remove-category-base-from-permalinks-45.htm
October 22nd, 2007 at 5:17 pm
[…] Кому интересно посушить мозги, вот ссылочки: Change or remove the “Category” from your permalinks on a wordpress blog How to remove category base from permalinks Removing the Dreaded Wordpress “Category” Forever! WordPress Ideas: Remove ‘Category Base’ From Permalinks WordPress Support: how could i remove word “category” from urls? […]
May 19th, 2008 at 9:06 am
[…] Brian Shoff schlägt zur Entfernung der Category Base in einem anderen Artikel noch einen Eingriff in den Code vor und überall sind Hacks im Einsatz. […]
August 18th, 2008 at 9:36 am
[…] Massive thanks to brianshoff.com for the original code snippet. […]
September 26th, 2008 at 1:43 am
Hi,
It worked for me when i did as you said
but i am getting the links as http://www.localsadda.com/softwares
The last slash is not coming.
How can i get the ‘/’ at the end of the category i.e., after word softwares in the above link.
Regards,
Sunil.
September 26th, 2008 at 12:56 pm
It’s questions like these that make me wish I was a *real* programmer.
To take a stab at it, as I don’t have time at the moment to test this, but wanted to get back to you quickly… buuuuut… next to the catlink in the str_replace function, add:
. ‘/’
This *might* append the trailing slash you’re looking for.
Or, if you are slick with .htaccess, you could do a 301 redirects of sorts to accomplish the same thing (I’m about 50/50 on that suggestion)
Hope this helps a little. :-/
October 6th, 2008 at 7:45 am
i think you could also use the permalink settings in your wp-admin to change the way the url is generated for your categories and tags; i changed it the tag permalink into “discover_more”:
http://r-echos.net/discover_more/code/
and so far it works fine - i guess changing it for the category work - the same. this solution, like your plugin, has also the advantage of keeping your install updatable without too much hassle but it
October 29th, 2008 at 3:27 pm
Any ideas on how to make this work if we are just using %postname% for our permalink structure (no category in permalink)?
I am getting he following error because I am not including categories in my permalink:
Warning: Division by zero in wp-includes/category-template.php on line 43
Oh, and can you add a “subscribe to comments” plugin feature so I will be updated when you comment back please? Thanks.
October 31st, 2008 at 10:50 am
I’m getting 404’s on any /category/page/2/ url’s…any idea how to work around that?
October 31st, 2008 at 11:26 am
Nevermind….figured it out!
October 31st, 2008 at 3:58 pm
I’ve got the pagination problem fixed if anyone is having that issue.
http://www.themadhat.com/blogging/remove-category-base-wordpress/