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

get_the_category_list( string $separator = '', string $parents = '', int $post_id = false )

以HTML列表或自定义格式检索文章的类别列表

cat

category 分类more...

listmore...

themore...


描述

通常用于快速、分隔(如逗号分隔)的类别列表,作为文章页面的一部分。

有关更强大的基于列表的函数,请参见wp_list_categories()

另见


参数

$separator

(string) (可选) 类别之间的分隔符。默认情况下,链接放置在无序列表中。空字符串将导致默认行为。

默认值: ''

$parents

(string) (可选) 如何显示父对象。

默认值: ''

$post_id

(int) (可选) 要检索类别的文章ID

默认值: false


返回

(string) 文章的类别列表



源码

查看源码 官方文档


更新日志

版本描述
1.5.1开始引入

使用示例

  • 示例1

    显示为列表项
    $separator值保留为空,将生成一个无序列表,并包含css类。

    <?php echo get_the_category_list(); ?>
    

    结果:

    <ul class="post-categories">
    	<li>
    		<a href="http://myblog.com/category/business" title="View all posts in Business" rel="category tag">Business</a>
    	</li>
    </ul>
    
    
  • 示例2

    content-single.php中的WordPress默认Twenty Eleven主题的实现:

    <?php
    /* translators: used between list items, there is a space after the comma */
    $categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
    
    /* translators: used between list items, there is a space after the comma */
    $tag_list = get_the_tag_list( '', __( ', ', 'twentyeleven' ) );
    if ( '' != $tag_list ) {
    	$utility_text = __( 'This entry was posted in %1$s and tagged %2$s by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
    } elseif ( '' != $categories_list ) {
    	$utility_text = __( 'This entry was posted in %1$s by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
    } else {
    	$utility_text = __( 'This entry was posted by <a href="%6$s">%5$s</a>. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyeleven' );
    }
    
    printf(
    	$utility_text,
    	$categories_list,
    	$tag_list,
    	esc_url( get_permalink() ),
    	the_title_attribute( 'echo=0' ),
    	get_the_author(),
    	esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) )
    );
    ?>