【发布时间】:2010-03-08 16:26:20
【问题描述】:
有没有办法在 wordpress 循环内分别获取类别名称和类别页面链接。我也没有类别的 ID,我想显示图像而不是类别名称,因此 the_category() 对我不起作用。
谢谢
感谢所有答案..
【问题讨论】:
有没有办法在 wordpress 循环内分别获取类别名称和类别页面链接。我也没有类别的 ID,我想显示图像而不是类别名称,因此 the_category() 对我不起作用。
谢谢
感谢所有答案..
【问题讨论】:
你可以使用:
$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>';
或者:
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() 获得类别的链接。
【讨论】:
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)
现在您可以像这样遍历每个类别
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
}
【讨论】:
循环中
<?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>'
?>
【讨论】:
我认为 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>'
?>
$category 应该是 $categories,那么它对我有用
【讨论】:
这段代码很好,只是你忘了放另一个;在链接的末尾
echo <a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>
应该是
echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ;
【讨论】: