Skip to content

Instantly share code, notes, and snippets.

@jahir07
Forked from enricodeleo/taxonomies.php
Created August 28, 2020 06:56
Show Gist options
  • Save jahir07/87e8964115f636a958ab844a428e5ac5 to your computer and use it in GitHub Desktop.
Save jahir07/87e8964115f636a958ab844a428e5ac5 to your computer and use it in GitHub Desktop.
Get and list all the taxonomies attached to a custom post type in Wordpress
<?php
$taxonomy_objects = get_object_taxonomies( 'cpt', 'objects' ); // <--- change cpt with the custom post type
$out = array();
foreach ( $taxonomy_objects as $taxonomy_slug => $taxonomy ){
$terms = get_terms( $taxonomy_slug, 'hide_empty=0' );
if ( !empty( $terms ) ) {
$out[] = "<strong>" . $taxonomy->label . "</strong>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
" <li>"
. $term->name
. " </li>\n";
}
$out[] = "</ul>\n";
}
}
echo implode('', $out );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment