6

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();

4

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 ) ?>
3
  • 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
4

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);
} } ?>
2

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.

1
  • For your images, you should use CSS pseudo selectors to inject your icons. Sep 21 '11 at 13:30
1

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
0
echo get_the_term_list( get_the_ID(), 'tax_name', 'Product:' );
0

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

A bit of update on the subject as this question is about 11 years old, and nobody mentioned it.


You can use get_query_var().

Retrieves the value of a query variable in the WP_Query class. to get the current term being queried on a taxonomy page.

Instead of doing a other query, we're just fetching the term from the current one. Then we can echoed it out on the front-end.

<?= get_query_var( 'term' ); ?>

Your Answer

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