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