I want to echo the term slug for each post on an archive page.

To get the term slug i'm using the following code:

$terms = get_the_terms( get_the_ID(), 'type_kennisbank' );

That returns me the data that I want:

array(1) { [0]=> object(WP_Term)#18648 (11) { ["term_id"]=> int(28) ["name"]=> 
string(10) "Whitepaper" ["slug"]=> string(10) "whitepaper" ["term_group"]=> 
int(0) ["term_taxonomy_id"]=> int(28) ["taxonomy"]=> string(15) 
"type_kennisbank" ["description"]=> string(0) "" ["parent"]=> int(0)  
["count"]=> int(3) ["filter"]=> string(3) "raw" ["term_order"]=> string(1) "0" } }

If I want to echo the slug I'm using:

echo $terms["slug"]

However it returns nothing. I already found a solution to echo the term slug but I'm wondering why my own echo code doesn't return the slug.

Anyone who can explain this?

  • $terms is an array and each item is an object. E.g. For the data in your question, you can do echo $terms[0]->slug; to display the term slug.
    – Sally CJ
    Dec 12, 2018 at 14:49
  • You're welcome. :) And be sure to check Hector's answer, because he's correct - get_the_terms() could return a WP_Error instance, or that the array could contain two or more items. All the best!
    – Sally CJ
    Dec 13, 2018 at 2:03

1 Answer 1


The get_the_terms function returns array of WP_Term objects. So you need to use something like this to echo a single term slug:

echo $terms[0]->slug;

Also be aware of results of this function. As documentation says it returns:

Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.

So you need some checks before trying to echo terms. The following code may help you.

$terms = get_the_terms( get_the_ID(), 'type_kennisbank' );

if ( $terms && ! is_wp_error( $terms ) ) {
    foreach ( $terms as $term ) {
        echo $term->slug;
  • 1
    Yes that's the solution that I found as well in the Codex. However I only needed to echo the slug to add it as a CSS-class. But I agree about the check in advance before outputting the slug. Thanks!
    – NielsPilon
    Dec 13, 2018 at 9:04

Your Answer

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

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