Get the category name and category link inside the loop in WordPress

Is there a way to get the category name and link to the category page separately inside the wordpress loop . I also don't have a category identifier, and I want to display images instead of category names, so the_category () does not work for me.

thank

Rate all the answers ..

+3
source share
5 answers

get_the_category()works in LOOP. Using this, you will get an array of the category object for each message that is currently processing the loop. 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, for example

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
}
+11
source

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 will help you.

:

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>';
}

get_the_category() get_category_link ( ), .

, :)

+6

In the loop

<?php
global $post;
$categories = get_the_category($post->ID);
$cat_link = get_category_link($category[0]->cat_ID);
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>'
?>
0
source

I think the strategy code should be changed as follows:

 <?php
 global $post;
 $categories = get_the_category($post->ID);
 $cat_link = get_category_link($categories[0]->cat_ID);
 echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' 
 ?>

$ category should be $ category, then it works for me

0
source

This code is good, except that you forgot to put another; at the end of the link

echo <a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>

it should be

 echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ;
0
source

Source: https://habr.com/ru/post/1735957/


All Articles