Codex tools: Log in
Contents |
Retrieve the terms in taxonomy or list of taxonomies.
<?php get_terms( $taxonomies, $args ) ?>
<?php
// no default values. using these as examples
$taxonomies = array(
'post_tag',
'my_tax',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => ,
'fields' => 'all',
'slug' => ,
'parent' => ,
'hierarchical' => true,
'child_of' => 0,
'get' => ,
'name__like' => ,
'pad_counts' => false,
'offset' => ,
'search' => ,
'cache_domain' => 'core'
);
?>
NOTE: Arguments are passed in the format used by wp_parse_args(). e.g.
NOTE: The difference between 'search' and 'name__like' is the leading '%' in the LIKE clause. So search is '%search%' and name__like is 'name__like%'
The fields returned are:
Get all post categories ordered by count.
String syntax:
$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );
Array syntax:
$categories = get_terms( 'category', array( 'orderby' => 'count', 'hide_empty' => 0 ) );
Get all the links categories:
$mylinks_categories = get_terms('link_category', 'orderby=count&hide_empty=0');
List all the terms in a custom taxonomy, without a link:
$terms = get_terms("my_taxonomy");
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
List all the terms, with link to term archive, separated by an interpunct (·). (language specific, WPML method):
$args = array( 'taxonomy' => 'my_term' );
$terms = get_terms('my_term', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$i++;
$term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
if ($count != $i) $term_list .= ' · '; else $term_list .= '</p>';
}
echo $term_list;
}
You can inject any customizations to the query before it is sent or control the output with filters.
The 'get_terms' filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args.
This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.
The 'list_terms_exclusions' filter passes the compiled exclusions along with the $args.
get_terms() is located in wp-includes/taxonomy.php.