获取类别名称 &WordPress 循环内的类别链接 [英] Get The Category Name & Category Link Inside The Loop In WordPress

查看:33
本文介绍了获取类别名称 &WordPress 循环内的类别链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在wordpress循环中分别获取类别名称和类别页面的链接.我也没有类别的 id,我想显示图像而不是类别名称,因此 the_category() 对我不起作用.

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.

谢谢

感谢所有答案..

推荐答案

get_the_category() 在 THE LOOP 中工作.使用它,您将获得循环当前正在处理的每个帖子的类别对象数组.示例:

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
}

这篇关于获取类别名称 &amp;WordPress 循环内的类别链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆