ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

wordpress检索分类法函数:get_terms

2022-06-24 12:02:58  阅读:198  来源: 互联网

标签:count term terms name get wordpress 默认值


wordpress检索分类法函数:get_terms

说明

检索分类法或分类法列表中的term

用法

  1. get_terms($taxonomies, $args = )

传递变量按 wp_parse_args()等函数所用的格式。

  1. $myterms = get_terms("orderby=count&hide_empty=false");

未指定值的变量使用以下默认值(下文中有说明)。下面的列表中含有$args,将改写默认值。

  1. $args = array(
  2. 'orderby' => 'name',
  3. 'order' => 'ASC',
  4. 'hide_empty' => true,
  5. 'exclude' => array(),
  6. 'exclude_tree' => array(),
  7. 'include' => array(),
  8. 'number' => ,
  9. 'fields' => 'all',
  10. 'slug' => ,
  11. 'parent' => ,
  12. 'hierarchical' => true,
  13. 'child_of' => 0,
  14. 'get' => ,
  15. 'name__like' => ,
  16. 'pad_counts' => false,
  17. 'offset' => ,
  18. 'search' => ,
  19. 'cache_domain' => 'core'
  20. );

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’排序

字符串参数格式

  1. $categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

数组参数格式:

  1. $categories = get_terms( 'category', array(
  2. 'orderby' => 'count',
  3. 'hide_empty' => 0
  4. ) );

获取所有友情链接的分类:

  1. $mylinks_categories = get_terms('link_category', 'orderby=count&hide_empty=0');

列出所有不带链接的自定义分类:

  1. $terms = get_terms("my_taxonomy");
  2. $count = count($terms);
  3. if ( $count > 0 ){
  4. echo "<ul>";
  5. foreach ( $terms as $term ) {
  6. echo "<li>" . $term->name . "</li>";
  7.  
  8. }
  9. echo "</ul>";
  10. }

列出所有带上链接的自定义分类:

  1. $args = array( 'taxonomy' => 'my_term' );
  2.  
  3. $terms = get_terms('my_term', $args);
  4.  
  5. $count = count($terms); $i=0;
  6. if ($count > 0) {
  7. $cape_list = '<p class="my_term-archive">';
  8. foreach ($terms as $term) {
  9. $i++;
  10. $term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
  11. if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
  12. }
  13. echo $term_list;
  14. }

源代码

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

标签:count,term,terms,name,get,wordpress,默认值
来源: https://www.cnblogs.com/ccw869476711/p/16408275.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有