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 need to display the current term in my custom taxonomy in a single post.

example:

  • My custom taxonomy is products and term of them is product-1, product-2 and products-3.
  • My post is assigned to product-2
  • And I want to print the current products = products-2 in my post

In fact, I need a function like WordPress's the_category(); but for my taxonomy like the_customtaxonomy();

UPDATE :

in facts i know i need to get id of this becuse i need to show a icon for this in my single , for example a function like the_category_ID();

share|improve this question
add comment

migrated from stackoverflow.com Feb 15 '11 at 16:07

This question came from our site for professional and enthusiast programmers.

5 Answers

You can use get_the_term_list():

Description

Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.

Usage

<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
share|improve this answer
1  
the only problem with get_the_term_list is that it wraps the terms in html. use wp_get_object_terms() to get an array of terms back –  anu Feb 15 '11 at 16:38
    
In that case you could use get_the_terms instead. –  t31os Feb 15 '11 at 16:49
    
@anu, Well, he did say he wanted a function like the_category, which also wraps the terms in HTML. –  user2370 Feb 15 '11 at 18:08
add comment

I found it:

<?php 
//list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

$taxonomy     = 'genre';
$orderby      = 'name'; 
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';

$args = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title
);
?>

<ul>
<?php wp_list_categories( $args ); ?>
</ul>

It gets all terms in my custom taxonomy and I need to get current term.

share|improve this answer
    
For your images, you should use CSS pseudo selectors to inject your icons. –  Brian Fegter Sep 21 '11 at 13:30
add comment

tanks for answer from my friend , i find it for show slug of my taxonomy

<?php
 $terms = get_terms('my-taxonomy-name');
 foreach ( $terms as $term ) {
 echo $term->slug.' ';
 }
?>

and but it return all term in my taxonomy and i need to return current term in my taxonomy ..

UPDATE :

i finaly find this and add if for empty terms and works

<?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);
} } ?>
share|improve this answer
add comment

You need to use wp_get_object_terms()

wp_get_object_terms( $object_ids, $taxonomies, $args )

  • $object_ids: string or array ids for the objects you want to get terms for
  • $taxonomies: string or array of taxonomies
share|improve this answer
add comment
echo get_the_term_list( get_the_ID(), 'tax_name', 'Product:' );
share|improve this answer
add comment

Your Answer

 
discard

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