3

Use case:

I have a custom post type of 'show' for a music venue. In the sidebar we want to display any related 'show' to the current one on single-show.php. They will be related by a custom taxonomy of 'genre'. I figured I could dump the custom taxonomy slugs for a post (might be more than one) into a variable then pass that variable into a custom query for the sidebar post.

Using get_the_term_list() works if there is a single taxonomy but if there are multiple it breaks (of course).

Any thoughts on how to get an array of the custom taxonomy slugs for a give post into a variable?

4

You can do something like the following:

$terms = get_the_terms( $post->id, 'genre' ); // get an array of all the terms as objects.

$terms_slugs = array();

foreach( $terms as $term ) {
    $terms_slugs[] = $term->slug; // save the slugs in an array
}

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.