How to remove link from the_category in WordPress

Powered by WordPressRecently I’ve been delving into customising both WordPress and Drupal and although I’d consider myself still a beginner, I still think it’s worth writing about problems I’ve encountered so others learning WordPress & Drupal customisation, can do it without the same frustrations.

So today I’m going to talk about how I needed to include a post category on a post page. A client required each post to have a header above the post with the posts category name nicely styled with CSS above the post. After a read though the WordPress documentation and some experimentation, I ended up with the the_category tag and what I thought would be the solution to my problem.

Unfortunately, this wasn’t entirely correct as the_category tag causes the category name to be a link text as well which wasn’t what I wanted. So I dugg around the WordPress docs and forums to try to find how this could be done. It wasn’t long before I found others looking for the same solution and responded to their own problem post with… ‘it’s ok, I’ve worked it out’. That’s right… they didn’t put how they solved the problem which is why I couldn’t resist writing about this. So here is the solution…

Instead of using the_category, you use get_the_category as follows

<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>

get_the_category tag comes with other objects that are available from the array.

  1. cat_ID – category id
  2. cat_name – category name
  3. category_nicename -post slug
  4. category_description – category description
  5. category_parent – the current categories parent category id
  6. category_count – the number of uses of this category

You can find more information at the url below.

http://codex.wordpress.org/Template_Tags/get_the_category

Whilst looking back in hind sight, this seem pretty obvious… when your trawling through documents, blog posts, forums, you find yourself missing the critical piece of the puzzle. Hope this helps…:-)
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

About Robert

Freelance Web Designer Sydney FX Pty Ltd
This entry was posted in Wordpress Customisation and tagged , , . Bookmark the permalink.

6 Responses to How to remove link from the_category in WordPress

  1. Fred says:

    THANKS A LOOOOOOOOOOOOOOOOOOOOOOTTTT!!!! I mean THANKS!! Really! It would be great to know how to modify the php functions to have them behave any way I wanted, but I don’t know squat of php

  2. Thanks for the little trick.
    I found it useful to implement it in my site.

  3. Ricky says:

    Thanks a bunch man!

    Been searchin’ for this exact thing. And just worked out perfect for something I was working on.

    Thank you again!

  4. Hugo says:

    Thanks you save me hours of searching, great tip for customisation. Keep the good work. If you come to portugal i´ll pay you a pint.

  5. Dustin says:

    Thanks a ton. I was thinking about just keeping it a link and changing the CSS. Your way is the way I thought it should be. Thanks again!