WordPress at a glance

get_the_taxonomies() WP 2.5.0

Retrieve all taxonomies associated with a post. Gets an array of HTML links.

Get taxonomies as an array the values of which contains HTML links to the specified post terms.

This function is useful when you want to show all links to all terms of all taxonomies to which the post is attached.

This function can be used within the loop.

Works with posts, custom post types, and custom taxonomies — full set...

Use the wrapper of this function — the_taxonomies() to get the result of a finished string for display, not an array.

Is the basis for: the_taxonomies()
✈ 1 time = 0.004388s = very slow | 50000 times = 114.09s = extremely slow | PHP 7.0.8, WP 4.6

No Hooks.

Return

Array. list of taxonomies, where the array key is a taxonomy slug and the value is a list of terms links.

Usage

get_the_taxonomies( $post, $args );
$post(int/WP_Post)
Post ID or WP_Post object.
Default: global $post
$args(array)

Arguments about how to format the list of taxonomies.

  • $template (string)
    Template for displaying a taxonomy label and list of terms.
    Default: "Label: Terms."

  • $term_template (string)
    Template for displaying a single term in the list.
    Default: term name linked to its archive

Default: empty array

Examples

#1 Demonstration of work for a simple post

$taxes = get_the_taxonomies( 119 );

/* $taxes will contain:
Array
(
	[category] => Categories: <a href="URL">Codex</a>.
	[post_tag] => Tags: <a href="URL">loop</a> and <a href="URL">Newbe</a>.
)
*/

#2 Demonstration of work for a post type with taxonomies

$taxes = get_the_taxonomies( 7363 );

/* $taxes will contain:
Array
(
	[wpfunctag] => Function tags: <a href="URL">Upload</a>.
	[wpfunccat] => Function categores: <a href="URL">Others</a>.
)
*/

#3 Change the output format

To change the output format, specify the following parameters:

$taxes = get_the_taxonomies( 119, array(
	'template'      => 'Taxonomy "%s": %l',
	'term_template' => '<a href="%1$s" target="_blank">%2$s</a>',
) );

/* $taxes will contain:
Array
(
	[category] => Taxonomy "Cats": <a href="URL" target="_blank">Codex</a>
	[post_tag] => Taxonomy "Tags": <a href="URL" target="_blank">loop</a> and <a href="URL" target="_blank">Newbe</a>
)
*/

#4 Let's collect everything and display it in LI list

This is the analogue of the the_taxonomies() function.

$taxes = get_the_taxonomies( 119 );

if( $taxes )
	echo "<ul>\n\t<li>". implode("</li>\n\t<li>", $taxes ) ."</li>\n</ul>";

Result:

<ul>
	<li>Categories: <a href="URL">Codex</a>.</li>
	<li>Tags: <a href="URL">loop</a> and <a href="URL">Newbe</a>.</li>
</ul>

Changelog

Since 2.5.0 Introduced.

Code of get the taxonomies: wp-includes/taxonomy.php WP 5.2.4

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

Related Functions

From category: For posts

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register