当前浏览:首页 / WordPress函数 / the_terms()

the_terms( int $post_id, string $taxonomy, string $before = '', string $sep = ', ', string $after = '' )

在列表中显示文章的分类项(term)

term 分类项more...

themore...


参数

$post_id

(int)(必填) 文章 ID

$taxonomy

(string)(必填) 分类法名称(taxonomy name)

$before

(string)(可选) 要在term之前使用的字符串。

默认值: ''

$sep

(string)(可选) 要在term之间使用的字符串。

默认值: ', '

$after

(string)(可选) 要在term后面使用的字符串。

默认值: ''


返回

(void|false) 成功时无返回,失败为false。



源码

查看源码 官方文档


更新日志

版本描述
2.5.0开始引入

使用示例

  • 示例1

    获取当前文章的类别
    获取当前文章的类别列表:

    <?php the_terms( $post->ID, 'category', 'categories: ', ' / ' ); ?>
    

    上述代码的输出可以是:categories: TV / Speaker / Monitor

  • 示例2

    我只是想补充一点,上面的代码将以href链接的形式输出类别。

    E.g.代码输出:

    <?php the_terms( $post->ID, 'category', 'categories: ', ' / ' ); ?>

    将会是以下形式:
    categories: TV / Speaker / Monitor