Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.


I'm trying to use get_the_term_list to catch the name of the current taxonomy of the page, and then use it with get_terms and a foreach method, to show the result of all the "elements" from the same taxonomy, but i only got an empty result. (for example, this is to have the related articles of the page).

Do you know why it doesn't work? The echo on get_the_term_list works ok, but then in the parameter of get_terms, the result of the "li"s is blank.

$my_tax = get_the_term_list( $post->ID, 'type');
//echo $my_tax;?> output works fine

$terms = get_terms($my_tax);
foreach ($terms as $term) {
echo "<li>".$term->name."</li>"; // empty
}

Could you help me?

share|improve this question

2 Answers 2

get_the_term_list() is retrieving the terms attached to the post that are in the taxonomy "types".

get_terms() is designed to retrieve all the terms within a taxonomy.

What you are trying to do is use get_terms() for a specific term in a taxonomy, not a taxonomy itself.

You need to do: $terms = get_terms('type');

share|improve this answer
    
@kaiser : thanks - but what if i don't want to write the 'type' of the page myself, and if i want it to automatically know the 'term', and THEN find the other elements into this precise term? would it be possible? otherwise i'll need to write each time the 'type', which is not really practical... –  Paul_p May 25 '11 at 12:27
    
You can use get_taxonomies() to store the taxonomy object in a variable codex.wordpress.org/Function_Reference/get_taxonomies and then do get_terms() on the taxonomy name. –  Pippin May 25 '11 at 13:43
    
@Pippin: I'm not the author of this A. I just edited the code formatting when reading the Q & A. –  kaiser May 25 '11 at 14:10
    
@Pippin : Thanks, i don't find how to reach "the taxonomy of the current post" ? Do the parameters in get taxonomy allow it? –  Paul_p May 25 '11 at 18:52
    
@kaiser : ok thanks for forwarding! –  Paul_p May 25 '11 at 18:54

get_post_taxonomies( $post->ID ) allows you to dynamically retrieve the names of the taxonomies attached to a post.

So you could retrieve ALL of the terms that belong to the current post's taxonomies:

$all_terms = get_terms( get_post_taxonomies( $post->ID ) );

OR just the terms that are assigned to the current post:

$object_terms = wp_get_object_terms( $post->ID, get_post_taxonomies( $post->ID ) );

I'll take it a step further and implement get_the_term_list() for each taxonomy:

foreach( get_post_taxonomies( $post->ID ) as $taxonomy ) {
    $taxonomy_name = get_taxonomy( $taxonomy )->labels->name;
    echo get_the_term_list( $post->ID, $taxonomy, '<h3>' . $taxonomy_name . '</h3><ul><li>', '</li><li>', '</li></ul>' );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.