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

问题描述 投票:4回答:5

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

谢谢

欣赏所有答案..

php wordpress
5个回答
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
}

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


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

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


0
投票

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

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

应该

 echo '<a href="'.$cat_link.'">'.$categories[0]->cat_name.'</a>' ;
© www.soinside.com 2019 - 2024. All rights reserved.