On this page
get_the_taxonomies( int|WP_Post $post, array $args = array() ): string[]
Retrieves 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.
Parameters
$postint|WP_Post Optional-
Post ID or WP_Post object. Default is global $post.
$argsarray Optional-
Arguments about how to format the list of taxonomies.
templatestringTemplate for displaying a taxonomy label and list of terms.
Default is "Label: Terms."term_templatestringTemplate for displaying a single term in the list. Default is the term name linked to its archive.
Default:
array()
Return
string[] List of taxonomies.
Source
File: wp-includes/taxonomy.php. View all references
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
Uses
| Uses | Description |
|---|---|
| wp_sprintf() wp-includes/formatting.php | WordPress implementation of PHP sprintf() with filters. |
| get_object_term_cache() wp-includes/taxonomy.php | Retrieves the cached term objects for the given object ID. |
| get_term_link() wp-includes/taxonomy.php | Generates a permalink for a taxonomy term archive. |
| wp_get_object_terms() wp-includes/taxonomy.php | Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
| get_object_taxonomies() wp-includes/taxonomy.php | Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| get_taxonomy() wp-includes/taxonomy.php | Retrieves the taxonomy object of $taxonomy. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| the_taxonomies() wp-includes/taxonomy.php | Displays the taxonomies of a post with available options. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_the_taxonomies