wordpress检索分类法函数:get_terms

说明

检索分类法或分类法列表中的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'
);


orderby — 默认值为’name’,可以是名称,计数或空(使用term_id)

order — 默认值为ASC。有效值也包括DESC。

hide_empty — 默认值为true。不返回空$terms。

fields — 默认值为all。

slug — 任何含有slug的term都可以作为该变量的值。默认为空字符串。

hierarchical — 是否返回层级分类法。默认值为true。

name_like — 默认值为空字符串。

pad_counts — 默认值为FALSE。值为true时将计算包括$terms在内的所有子辈。

get — 默认值为空。可通过为’all’赋值来改写’hide_empty’和’child_of’。

child_of — 默认值为0。获取该term的所有后代。

parent — 默认值为0。获取该term的直系子辈(即上辈明确为该值的term)。

应用

获取所有分类按‘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 .= ' &middot; '; else $term_list .= '</p>';
}
echo $term_list;
}



源代码

get_terms() 位于 wp-includes/taxonomy.php

欢迎关注我的公众号“xx主题网”,原创技术文章第一时间推送。

文章来源:https://www.xxzhuti.com/587.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值