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.

Well, this should be pretty simple, however I couldn't find answer anywhere on the web. all answers I found were close but no exactly what I needed. what I need is to display just the current term of a custom post type I am in. not all the terms just one! (the relevant one)

this is what I'm using but it displays ALL the terms which is not good for me:

<?php
$taxonomy = 'genre';
$queried_term = get_query_var($taxonomy);
$terms = get_terms($taxonomy, 'slug='.$queried_term);
if ($terms) {
  foreach($terms as $term) {
    echo $term->name;
  }
}
?>

remember - I would like to display it in my single post type template can anyone suggest? thanks

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Ok, so I finally found what I needed here: How to get current term in my custom taxonomy in WordPress?

the last update at the bottom courtesy of @user3208:

<?php   // Get terms for post
 $terms = get_the_terms( $post->ID , 'oil' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->slug ;
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?>

That solved my issue! Thanks

share|improve this answer
add comment

You should use wp_get_post_terms instead.

$terms = wp_get_post_terms( $post_id, $taxonomy, $args );

get_terms will give you all the terms present in a taxonomy.

UPDATE:

global $post;
$terms = wp_get_post_terms( $post->ID, 'genre');
print_r($terms); #displays the output
share|improve this answer
    
I am trying but it's not working. do I have to pass any vars into the function? can you specify how should I implement it in my code? Thanks –  gil hamer Mar 1 '13 at 12:46
    
If you are in the WordPress Loop, you can use get_the_ID() instead of $post_id. For $taxonomy, you need to add the name of the taxonomy you're using. $args isn't necessary. –  RRikesh Mar 1 '13 at 16:14
    
It's definitely outside the loop! just cant get it to work.. can you suggest how to implement it outside the loop? if necassary I will post the whole code. Thanks –  gil hamer Mar 1 '13 at 16:28
    
Then you need to add global $post; and then use $post->ID to get the post ID. –  RRikesh Mar 1 '13 at 16:32
    
can you show me an example using my code above? I am not that programmer. I'll appreciate it. Thanks –  gil hamer Mar 1 '13 at 16:35
show 3 more comments

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.