Codex tools: Log in
Contents |
Retrieve the terms of the taxonomy that are attached to the post.
<?php get_the_terms( $id, $taxonomy ); ?>
Each term object will contain the following fields:
stdClass Object
(
[term_id] =>
[name] =>
[slug] =>
[term_group] =>
[term_order] =>
[term_taxonomy_id] =>
[taxonomy] =>
[description] =>
[parent] =>
[count] =>
[object_id] =>
)
Echoing the list of terms (for a taxonomy called on-draught). This is similar to the output from get_the_term_list, but without the terms being hyperlinked:
<?php
$terms = get_the_terms( $post->ID, 'on-draught' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<p class="beers draught">
On draught: <span><?php echo $on_draught; ?></span>
</p>
<?php endif; ?>
place this function in theme functions.php
<?php // get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $taxonomy) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term )
$out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
$return = join( ', ', $out );
}
}
return $return;
} ?>
Now you can use this function in your themes for your post types without the need to input anything
<?php echo custom_taxonomies_terms_links(); ?>
It uses get_object_term_cache() or wp_get_object_terms() to retrieve results.
get_the_terms() is located in wp-includes/category-template.php.