get_the_taxonomies

函数
get_the_taxonomies ( $post = 0, $args = array() )
参数
  • (int|WP_Post) $post Optional. Post ID or WP_Post object. Default is global $post.
    Required:
  • (array) $args { Optional. Arguments about how to format the list of taxonomies. Default empty array. @type string $template Template for displaying a taxonomy label and list of terms. Default is "Label: Terms." @type string $term_template Template for displaying a single term in the list. Default is the term name linked to its archive. }
    Required:
    Default: array()
返回值
  • (string[]) List of taxonomies.
定义位置
相关方法
the_taxonomiesget_taxonomiesget_post_taxonomiesget_attachment_taxonomiesget_object_taxonomies
引入
2.5.0
弃用
-

get_the_taxonomies: 该函数返回当前文章或作为参数传递给它的文章的分类学对象数组。

检索与一个文章相关的所有分类标准。

这个函数可以在循环中使用。它还将返回一个带有分类法和名称链接的分类法数组。

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;
}

常见问题

FAQs
查看更多 >