列出分配给帖子的类别

时间:2016-12-16 作者:Arete

我想输出分配给帖子的不同类别。例如,假设一篇文章有三个类别:

下载游戏视频之前我用过<?php echo get_the_category_list(); ?> 但这将<a> 在a中<span> 而不是包装<span> 在a*<a>...

我想列出如下类别:

<a href="the_URL_of_the_category_downloads">
    <span class="the_name_of_the_category_downloads">the_name_of_the_category_downloads</span>
</a>

<a href="the_URL_of_the_category_games">
    <span class="the_name_of_the_category_downloads">the_name_of_the_category_games</span>
</a>

<a href="the_URL_of_the_category_videos">
    <span class="the_name_of_the_category_downloads">the_name_of_the_category_videos</span>
</a>
我不确定如何将其用于php,因为我对php的总体知识非常有限。

1 个回复
最合适的回答,由SO网友:Tunji 整理而成

您可以使用get_the_category( ) 相反

Example:

$categories = get_the_category();
foreach( $categories as $category) {
    $name = $category->name;
    $category_link = get_category_link( $category->term_id );

    echo "<a href=\'$category_link\'>
            <span class=" . esc_attr( $name) . "></span>
         </a>";
}