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.

1
  • I would love to see a plugin for this to list all categories when someone goes to the category slug link, similar to plugins that list all posts for an individual category. This would be a hit I think and good for SEO.
    – Kevin
    Commented Jan 24 at 14:32

3 Answers 3

0

you can try below code

$term = get_term( get_the_ID(), 'portfolio_category');
$slug = $term->slug;
echo  $slug;
2
  • I try this before. But return NULL.
    – martins
    Commented Aug 16, 2018 at 16:15
  • I think you were trying it on the single portfolio page. if not let me know where you were trying it. Commented Aug 17, 2018 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);
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;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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