检索分类法或分类法列表中的term。
- get_terms($taxonomies, $args = )
传递变量按 wp_parse_args()等函数所用的格式。
- $myterms = get_terms("orderby=count&hide_empty=false");
未指定值的变量使用以下默认值(下文中有说明)。下面的列表中含有$args,将改写默认值。
- $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'
- );
- ?>
详细介绍
在查询被送出前可自定义查询,也可用过滤器控制输出结果。
缓存具有该term,若要将已成立的term及$taxonomies、$args数组一同传递时,’get_terms’过滤器将被调用。
传递term数组前,若要将term数组和$taxonomies、$args一同传递,’get_terms’过滤器也将被调用。
‘list_terms_exclusions’过滤器传递编译的Exclusion和$args。
获取所有分类按‘count’排序
字符串参数格式:
- $categories = get_terms( 'category', 'orderby=count&hide_empty=0' );
数组参数格式:
- $categories = get_terms( 'category', array(
- 'orderby' => 'count',
- 'hide_empty' => 0
- ) );
获取所有友情链接的分类:
- $mylinks_categories = get_terms('link_category', 'orderby=count&hide_empty=0');
列出所有不带链接的自定义分类:
- $terms = get_terms("my_taxonomy");
- $count = count($terms);
- if ( $count > 0 ){
- echo "<ul>";
- foreach ( $terms as $term ) {
- echo "<li>" . $term->name . "</li>";
- }
- echo "</ul>";
- }
列出所有带上链接的自定义分类:
- $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;
- }
get_terms() 位于 wp-includes/taxonomy.php.