返回介绍

get_the_taxonomies()

发布于 2017-09-11 00:38:49 字数 3430 浏览 1120 评论 0 收藏 0

get_the_taxonomies( int|WP_Post $post,  array $args = array() )

Retrieve all taxonomies associated with a post.


description

This function can be used within the loop. It will also return an array of the taxonomies with links to the taxonomy and name.


参数

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.

$args

(array) (Optional) Arguments about how to format the list of taxonomies.

  • 'template'
    (string) Template for displaying a taxonomy label and list of terms. Default is "Label: Terms."
  • 'term_template'
    (string) Template for displaying a single term in the list. Default is the term name linked to its archive.

Default value: array()


返回值

(array) List of taxonomies.


源代码

File: wp-includes/taxonomy.php

function get_the_taxonomies( $post = 0, $args = array() ) {
	$post = get_post( $post );

	$args = wp_parse_args( $args, array(
		/* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
		'template' => __( '%s: %l.' ),
		'term_template' => '<a href="%1$s">%2$s</a>',
	) );

	$taxonomies = array();

	if ( ! $post ) {
		return $taxonomies;
	}

	foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
		$t = (array) get_taxonomy( $taxonomy );
		if ( empty( $t['label'] ) ) {
			$t['label'] = $taxonomy;
		}
		if ( empty( $t['args'] ) ) {
			$t['args'] = array();
		}
		if ( empty( $t['template'] ) ) {
			$t['template'] = $args['template'];
		}
		if ( empty( $t['term_template'] ) ) {
			$t['term_template'] = $args['term_template'];
		}

		$terms = get_object_term_cache( $post->ID, $taxonomy );
		if ( false === $terms ) {
			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
		}
		$links = array();

		foreach ( $terms as $term ) {
			$links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
		}
		if ( $links ) {
			$taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
		}
	}
	return $taxonomies;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_sprintf()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: get_object_term_cache()
  • wp-includes/taxonomy.php: get_term_link()
  • wp-includes/taxonomy.php: wp_get_object_terms()
  • wp-includes/taxonomy.php: get_object_taxonomies()
  • wp-includes/taxonomy.php: get_taxonomy()
  • wp-includes/post.php: get_post()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/taxonomy.php: the_taxonomies()

User Contributed Notes

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

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

发布评论

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