Get term slug of current post

Your code works on a page where a term is queried (a taxonomy term archive), not a single post.

For a single post, you need to fetch the terms belonging to that post.

$terms = get_the_terms( $post->ID, 'your-taxonomy' );
if ( !empty( $terms ) ){
    // get the first term
    $term = array_shift( $terms );
    echo $term->slug;
}

Leave a Comment