php 在 WordPress 的循环中获取类别名称和类别链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2402808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get The Category Name & Category Link Inside The Loop In WordPress
提问by Aayush
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.
有没有办法在 wordpress 循环中分别获取类别名称和类别页面的链接。我也没有类别的 id,我想显示图像而不是类别名称,因此 the_category() 对我不起作用。
Thanks
谢谢
Appreciate all the answers..
感谢所有的答案..
回答by Pragati Sureka
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 :
get_the_category()在循环中工作。使用它,您将获得循环当前正在处理的每个帖子的类别对象数组。例子 :
//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
}
回答by ahmet2106
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>';
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.
使用get_the_category()可以获得类别,使用get_category_link()可以获得类别的链接。
回答by Strateg
In 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>'
?>
回答by Sjoerd
I think Strateg's code should be changed like so:
我认为 Strateg 的代码应该像这样改变:
<?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>'
?>
the $category should be $categories, then it works for me
$category 应该是 $categories,然后它对我有用
回答by John Miles
This code is good except you forgot to put another ; at the end of the link
这段代码很好,只是你忘了放另一个;在链接的末尾
echo <a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>
should be
应该
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ;

