Skip to content

Instantly share code, notes, and snippets.

@thetwopct
Created December 22, 2018 05:46
Show Gist options
  • Save thetwopct/4795606310f8ec6963ab8a965299d9b8 to your computer and use it in GitHub Desktop.
Save thetwopct/4795606310f8ec6963ab8a965299d9b8 to your computer and use it in GitHub Desktop.
Show all custom post type taxonomies in a list with their posts (like Sitemap)
<?php
$object = 'treatments'; // CPT TYPE
$output = 'objects';
$taxonomies = get_object_taxonomies( $object, $output );
$exclude = array( 'post_tag', 'post_format' );
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
if( in_array( $taxonomy->name, $exclude ) ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => $taxonomy->name,
'hide_empty' => true,
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
echo '<h3>' . $taxonomy->label . '</h3>';
$term_list = '<ul class="term-list">';
foreach ( $terms as $term ) {
$term_list .= '<li><a href="' . esc_url( get_term_link( $term ) ) . '" >' . $term->name . '</a></li>';
}
$term_list .= '</ul>';
echo $term_list;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment