获取类别名称&在WordPress中循环内的类别链接

时间:2010-03-08 16:26:20

标签: php wordpress

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

由于

欣赏所有答案..

5 个答案:

答案 0 :(得分:11)

get_the_category()在THE LOOP中有效。使用此方法,您将获得循环当前正在处理的每个帖子的类别对象数组。例如:

//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
}

答案 1 :(得分:6)

您可以使用:

$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(),您将获得该类别的链接。

答案 2 :(得分:0)

循环

<?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>'
?>

答案 3 :(得分:0)

我认为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,然后它适用于我

答案 4 :(得分:0)

这个代码很好,除了你忘了放另一个;在链接的末尾

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

应该是

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