the_terms()显示一篇文章所属的分类信息

目录

描述

Displays a string of linked terms for a post.

the_terms() 描述

用法

 <?php the_terms$id$taxonomy$before$sep$after ); ?> 

the_terms() 用法

参数

$id

(integer) (mandatory) Post ID.

默认值: 0

$taxonomy

(string) (mandatory) Taxonomy name.

默认值: Empty string

$before

(string) (可选) Text to display before the actual tags are displayed.

默认值: Empty string

$sep

(string) (可选) Text or character to display between each tag link.

默认值: ', '

$after

(string) (可选) Text to display after the last tag.

默认值: Empty string

the_terms() 参数

返回值

(false

Will return false on failure. Otherwise, it displays the linked terms of a custom taxonomies separated by user defined characters, and will not return a value.

the_terms() 返回值

示例

Get Categories of Current Post

Get a list of current post's categories:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php the_terms( $post->ID, 'category', 'categories: ', ' / ' ); ?>

The output of above code will be: categories: TV / Speaker / Monitor

the_terms() 示例

注意

the_terms() 注意

历史

添加于 版本: 2.5.0

the_terms() 历史

源文件

the_terms() 函数的代码位于 wp-includes/category-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Display the terms in a list.
 *
 * @since 2.5.0
 *
 * @param int $id Post ID.
 * @param string $taxonomy Taxonomy name.
 * @param string $before Optional. Before list.
 * @param string $sep Optional. Separate items using this.
 * @param string $after Optional. After list.
 * @return false|void False on WordPress error.
 */
function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
	$term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );

	if ( is_wp_error( $term_list ) )
		return false;

	/**
	 * Filter the list of terms to display.
	 *
	 * @since 2.9.0
	 *
	 * @param array  $term_list List of terms to display.
	 * @param string $taxonomy  The taxonomy name.
	 * @param string $before    String to use before the terms.
	 * @param string $sep       String to use between the terms.
	 * @param string $after     String to use after the terms.
	 */
	echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
}

the_terms() 源文件

相关