views:

461

answers:

2

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..

A: 

You can use:

$category = get_the_category(); 
echo '<a href="'.get_category_link($category[0]->cat_ID).'"><img src="'.$category[0]->cat_name.'" alt="'.$category[0]->cat_name.'" /></a>';

I think this would help you

Or:

foreach(get_the_category() as $category)
{
    echo '<a href="'.get_category_link($category->cat_ID).'"><img src="'.$category->cat_name.'" alt="'.$category->cat_name.'" /></a>';
}

With get_the_category() you get the category, and with get_category_link() you'll get the Link of the Category.

I hope this helps you :)

ahmet2106
Thanks for the answer.but I don't think this would work for me.I need to know the category for each post the loop is currently processing. It's weird that there aren't any tags for these two things, when there is the_category().
Aayush
+1  A: 

get_the_category() works in THE LOOP. Using this you will get array of category object for each post the loop is currently processing. example :

//the loop
$categories = get_the_category();
//the loop cont....
var_dump($categories);
    array
      0 => 
        object(stdClass)[191]
          public 'term_id' => &string '1' (length=1)
          public 'name' => &string 'Uncategorized' (length=13)
          public 'slug' => &string 'uncategorized' (length=13)
          public 'term_group' => string '0' (length=1)
          public 'term_taxonomy_id' => string '1' (length=1)
          public 'taxonomy' => string 'category' (length=8)
          public 'description' => &string '' (length=0)
          public 'parent' => &string '0' (length=1)
          public 'count' => &string '1' (length=1)
          public 'object_id' => string '66' (length=2)
          public 'cat_ID' => &string '1' (length=1)
          public 'category_count' => &string '1' (length=1)
          public 'category_description' => &string '' (length=0)
          public 'cat_name' => &string 'Uncategorized' (length=13)
          public 'category_nicename' => &string 'uncategorized' (length=13)
          public 'category_parent' => &string '0' (length=1)
      1 => 
        object(stdClass)[190]
          public 'term_id' => &string '3' (length=1)
          public 'name' => &string 'asd' (length=3)
          public 'slug' => &string 'asd' (length=3)
          public 'term_group' => string '0' (length=1)
          public 'term_taxonomy_id' => string '3' (length=1)
          public 'taxonomy' => string 'category' (length=8)
          public 'description' => &string '' (length=0)
          public 'parent' => &string '0' (length=1)
          public 'count' => &string '1' (length=1)
          public 'object_id' => string '66' (length=2)
          public 'cat_ID' => &string '3' (length=1)
          public 'category_count' => &string '1' (length=1)
          public 'category_description' => &string '' (length=0)
          public 'cat_name' => &string 'asd' (length=3)
          public 'category_nicename' => &string 'asd' (length=3)
          public 'category_parent' => &string '0' (length=1)

now you can iterate through each category, like so

foreach($categories as $category){
   echo $category->name; //category name
   $cat_link = get_category_link($category->cat_ID);
   echo '<a href="'.$cat_link.'">'.$category->name.'</a>'; // category link
}
Pragati Sureka