get_the_taxonomies

The timeline below displays how wordpress function get_the_taxonomies has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param 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.
 * }
 * @return string[] List of 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;
}

WordPress Version: 5.5

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param 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.
 * }
 * @return array List of 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;
}

WordPress Version: 5.3

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param 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.
 * }
 * @return array List of 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;
}

WordPress Version: 4.3

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param 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.
 * }
 * @return array List of 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;
}

WordPress Version: 4.1

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param array $args {
 *     Arguments about how to format the list of taxonomies.
 *
 *     @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.
 * }
 * @return array List of 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;
}

WordPress Version: 4.0

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param array $args Override the defaults.
 * @return array List of 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 term links */
        'template' => __('%s: %l.'),
    ));
    $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'];
        }
        $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[] = "<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;
}

WordPress Version: 3.9

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object.
 * @param array $args Override the defaults.
 * @return array
 */
function get_the_taxonomies($post = 0, $args = array())
{
    $post = get_post($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 (false === $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;
}

WordPress Version: 3.7

/**
 * 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.
 *
 * @since 2.5.0
 *
 * @param int $post Optional. Post ID or will use Global Post ID (in loop).
 * @param array $args Override the defaults.
 * @return array
 */
function get_the_taxonomies($post = 0, $args = array())
{
    $post = get_post($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 (false === $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;
}