返回介绍

get_the_term_list()

发布于 2017-09-11 00:39:16 字数 4255 浏览 1070 评论 0 收藏 0

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

Retrieve a post’s terms as a list with specified format.


description


参数

$id

(int) (Required) Post ID.

$taxonomy

(string) (Required) Taxonomy name.

$before

(string) (Optional) Before list.

Default value: ''

$sep

(string) (Optional) Separate items using this.

Default value: ''

$after

(string) (Optional) After list.

Default value: ''


返回值

(string|false|WP_Error) A list of terms on success, false if there are no terms, WP_Error on failure.


源代码

File: wp-includes/category-template.php

function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
	$terms = get_the_terms( $id, $taxonomy );

	if ( is_wp_error( $terms ) )
		return $terms;

	if ( empty( $terms ) )
		return false;

	$links = array();

	foreach ( $terms as $term ) {
		$link = get_term_link( $term, $taxonomy );
		if ( is_wp_error( $link ) ) {
			return $link;
		}
		$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
	}

	/**
	 * Filters the term links for a given taxonomy.
	 *
	 * The dynamic portion of the filter name, `$taxonomy`, refers
	 * to the taxonomy slug.
	 *
	 * @since 2.5.0
	 *
	 * @param array $links An array of term links.
	 */
	$term_links = apply_filters( "term_links-{$taxonomy}", $links );

	return $before . join( $sep, $term_links ) . $after;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/category-template.php: term_links-{$taxonomy}
  • wp-includes/category-template.php: get_the_terms()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/taxonomy.php: get_term_link()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/category-template.php: the_terms()
  • wp-includes/category-template.php: get_the_tag_list()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    Used inside the loop this outputs the terms from the people taxonomy for a specific post.

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

    This would return something like.

    
    People: <a href="person1">Person 1</a>, <a href="person2">Person 2</a>, ...
    
  2. Returning an HTML List

    Used inside the loop this outputs the terms from the styles taxonomy for a specific post as an (x)html list.

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

    This would return something like.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文