8

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

4 Answers 4

16

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

0
8

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
8
  • 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, 2013 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, 2013 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, 2013 at 16:28
  • Then you need to add global $post; and then use $post->ID to get the post ID.
    – RRikesh
    Mar 1, 2013 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, 2013 at 16:35
2

Taking what user3208 coded, I have added a bit of code that adds the URL to the Term. Hope that helps someone out.

<?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 ) {
$term_link = get_term_link( $term, 'oil' );
 // Print the name and URL
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
// Get rid of the other data stored in the object, since it's not needed
unset($term); } } ?>
0
<?php echo get_the_term_list( $post->ID, 'yourtaxonomy', '', ', ' ); ?>
1
  • Code only answers are usually frowned upon without an explanation. Could you please edit your answer and explain what this function does and how this would solve the original problem, maybe linking to The Codex for more information?
    – Howdy_McGee
    Jul 10, 2017 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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