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

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

以指定格式检索文章的terms列表

listmore...

term 分类项more...

themore...


描述

获取文章的分类法项目(Terms)链接列表,可以是文章的类别、标签、自定义分类等,这些项目将链接到各自的项目列表页面。


参数

$post_id

(int) (必填) 文章ID

$taxonomy

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

$before

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

默认值: ''

$sep

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

默认值: ''

$after

(string) (可选) 要在项目后使用的字符串。

默认值: ''


返回

(string|false|WP_Error) 成功为项目列表,如果没有项目,则为false,失败为WP_Error



源码

查看源码 官方文档


更新日志

版本描述
2.5.0开始引入

使用示例

  • 示例1

    您可以将此功能与strip_tags功能一起使用,以打印未链接的项目列表:

    echo strip_tags( get_the_term_list( $post->ID, 'job_titles', '', ', ') )

    这将打印类似这样的结果:

    Designer, Front-end Developer, Developer

  • 示例2

    基本示例

    在循环中使用,这将从特定文章的“people”分类法中输出项目

    <?php echo get_the_term_list( $post->ID, 'people', 'People: ', ', ' ); ?>
    

    这将返回类似的结果:

    People: <a href="person1">Person 1</a>, <a href="person2">Person 2</a>, ...
    
  • 示例3

    返回HTML列表

    在循环中使用,将特定文章的“styles”分类法中的项目输出为 (x)html 列表。

    echo get_the_term_list( $post->ID, 'styles', '<ul class="styles"><li>', ',</li><li>', '</li></ul>' );
    

    这将返回类似的结果:

    <ul class="styles">
        <li><a href="person1">Style 1</a>,</li> 
        <li><a href="person2">Style 2</a></li>
    </ul>