0

I need to list all slug categories from a taxonomy portfolio_category. Like this:

the_terms( get_the_ID(), 'portfolio_category', '', ', ' );

This returns categories from a single portfolio. I want to return just slug of this.

0

you can try below code

$term = get_term( get_the_ID(), 'portfolio_category');
$slug = $term->slug;
echo  $slug;
|improve this answer|||||
  • I try this before. But return NULL. – martins Aug 16 '18 at 16:15
  • I think you were trying it on the single portfolio page. if not let me know where you were trying it. – 1naveengiri Aug 17 '18 at 2:53
0

Get terms attached to the post

$post_terms = get_the_terms(get_the_ID(), 'portfolio_category');
if( is_array($post_terms) ) {
    $post_terms = array_reduce($post_terms, function($carry, $item){
            $carry[] = $item->slug;
            return $carry;
        }, []);
    echo implode(', ', $post_terms);
}

Get all terms

To retrieve all terms from a given taxonomy, use function get_terms. Here you will find info about accepted arguments.

Since WP 4.5:

$portfolio_terms = get_terms([
    'taxonomy' => 'portfolio_category', 
    'hide_empty' => false,
    'fields' => 'id=>slug',
    'orderby' => 'slug',
]);
if( is_array($portfolio_terms) )
    echo implode(', ', $portfolio_terms);

Before WP 4.5:

$portfolio_terms = get_terms('portfolio_category', [
    'hide_empty' => false,
    'fields' => 'id=>slug',
    'orderby' => 'slug',
]);
if( is_array($portfolio_terms) )
    echo implode(', ', $portfolio_terms);
|improve this answer|||||
0

I got. I use this:

$post_id = get_the_ID();

$term = get_the_terms( get_the_ID(), 'portfolio_category');
foreach($term as $simples){
    echo $simples->slug;

}
|improve this answer|||||

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.