Is there a way to get the name of the category and the link to the category page separately inside the wordpress loop. I don't have the id of the category either and I wanna display images instead of category names therefore the_category() doesn't work for me.

Thanks

Appreciate all the answers...

Recommended Answers

All 4 Replies

To store the name of the category instead of outputting it straight to the screen, use get_the_category() to retrieve the categories for the current post. I think getting the link will depend on your permalinks settings:

<?php
$current_post_categories = get_the_category(); // store category objects for current post in an array
foreach( $current_post_categories as $category) {
   echo $category->cat_name; // print out the category name
   echo $category->cat_ID; // print out the category ID
   // if your permalinks are set to use the category's name for the URL, use:
   echo '<a href="http://your.wp.domain/'.$category->cat_name.'">this is a link</a>';
   // if you're not using nice links, I think it would be something like:
   echo '<a href="http://your.wp.domain/?cat='.$category->cat_ID.'">this is a link</a>';
}

Hi,

Thanx for this code.
It's very usefull.
I wrote it like a function cat_post_title().
It's work perfectly.
I tryed to tweak it, with a category filter.
After 4 hours i had no success...

Anyone have any idea ?

Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.