Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Last active June 14, 2019 13:08
Show Gist options
  • Save ajithrn/9730807 to your computer and use it in GitHub Desktop.
Save ajithrn/9730807 to your computer and use it in GitHub Desktop.
Wordpress: list custom taxonomy
<?php
/**
* list taxonomy accoeding to the custom post type
* @var [string]
* ref: http://codex.wordpress.org/Template_Tags/wp_list_categories
*/
$custom_taxonomy = get_object_taxonomies('taxonomy-slud-or-id');
if(count($custom_taxonomy) > 0):
foreach($custom_taxonomy as $tax):
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => ''
);
wp_list_categories( $args );
endforeach; // check taxonomy array display each
endif; // check tax count
?>
<?php
/**
* Alternateve method using taxonomy name
* ref: http://codex.wordpress.org/Function_Reference/get_terms
*/
$taxonomy = 'taxonomy-name';
$tax_terms = get_terms($taxonomy, array('orderby' => 'name', 'hide_empty' => 1, 'current_category' => 0 ) );
echo '<ul>';
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
echo '</ul>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment