get_the_taxonomies

Retrieve all taxonomies associated with a post.

Description

(array) get_the_taxonomies( (int) $post = 0, (array) $args = array() ); 

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

Returns (array)

List of taxonomies.

Parameters (2)

0. $post — Optional. (int)
Post ID or WP_Post object. Default is global $post.
1. $args — Optional. (array) => array()
Arguments about how to format the list of taxonomies. Default empty array.

Options

  • template (string) => ''

    Template for displaying a taxonomy label and list of terms. Default is Label: Terms.

array(

    /**
     * Template for displaying a taxonomy label and list of terms. Default is "Label: Terms."
     *
     * @type string
     * @default ''
     */
    'template' => ''
);        


Usage

  1. if ( !function_exists( 'get_the_taxonomies' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Optional. Post ID or WP_Post object. Default is global $post. 
  5. $post = -1; 
  6.  
  7. // Optional. Arguments about how to format the list of taxonomies. Default empty array. 
  8. $args = array( 
  9. 'template' => '' 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = get_the_taxonomies($post, $args); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function get_the_taxonomies( $post = 0, $args = array() ) { 
  2. $post = get_post( $post ); 
  3.  
  4. $args = wp_parse_args( $args, array( 
  5. /** translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */ 
  6. 'template' => __( '%s: %l.' ),  
  7. 'term_template' => '<a href="%1$s">%2$s</a>',  
  8. ) ); 
  9.  
  10. $taxonomies = array(); 
  11.  
  12. if ( ! $post ) { 
  13. return $taxonomies; 
  14.  
  15. foreach ( get_object_taxonomies( $post ) as $taxonomy ) { 
  16. $t = (array) get_taxonomy( $taxonomy ); 
  17. if ( empty( $t['label'] ) ) { 
  18. $t['label'] = $taxonomy; 
  19. if ( empty( $t['args'] ) ) { 
  20. $t['args'] = array(); 
  21. if ( empty( $t['template'] ) ) { 
  22. $t['template'] = $args['template']; 
  23. if ( empty( $t['term_template'] ) ) { 
  24. $t['term_template'] = $args['term_template']; 
  25.  
  26. $terms = get_object_term_cache( $post->ID, $taxonomy ); 
  27. if ( false === $terms ) { 
  28. $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] ); 
  29. $links = array(); 
  30.  
  31. foreach ( $terms as $term ) { 
  32. $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name ); 
  33. if ( $links ) { 
  34. $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms ); 
  35. return $taxonomies;