get_the_taxonomies

Definition:
function get_the_taxonomies($post = 0, $args = array() {}

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

Parameters

  • int $post: Optional. Post ID or will use Global Post ID (in loop).
  • array $args: Override the defaults.

Source code

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

	if ( is_int($post) )

		$post =& get_post($post);

	elseif ( !is_object($post) )

		$post =& $GLOBALS['post'];



	$args = wp_parse_args( $args, array(

		'template' => '%s: %l.',

	) );

	extract( $args, EXTR_SKIP );



	$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'] = $template;



		$terms = get_object_term_cache($post->ID, $taxonomy);

		if ( empty($terms) )

			$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);



		$links = array();



		foreach ( $terms as $term )

			$links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>";



		if ( $links )

			$taxonomies[$taxonomy] = wp_sprintf($t['template'], $t['label'], $links, $terms);

	}

	return $taxonomies;

}

1851

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: