经过三小时的源码分析,wordpress在处理递归层级结构时也是先将所有结果查询出来,然后用PHP处理
检索分类法或分类法列表中的term。
用法 get_terms($taxonomies, $args = ) 有效变量
传递变量按 wp_parse_args() 等函数所用的格式。
- $myterms = get_terms("orderby=count&hide_empty=false");
复制代码
未指定值的变量使用以下默认值(下文中有说明)。下面的列表中含有$args,将改写默认值。 - 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)。
详细介绍
在查询被送出前可自定义查询,也可用过滤器控制输出结果。
缓存具有该term,若要将已成立的term及$taxonomies、$args数组一同传递时,'get_terms'过滤器将被调用。
传递term数组前,若要将term数组和$taxonomies、$args一同传递,'get_terms'过滤器也将被调用。
'list_terms_exclusions'过滤器传递编译的Exclusion和$args。
taxonomy类型结构分类有 - category
- post_tag
- post_format
- nav_menu
- link_category
- $terms = get_terms( 'post_tag', array(
- 'hide_empty' => false,
- ) );
- $terms = get_terms( array(
- 'taxonomy' => 'post_tag',
- 'hide_empty' => false,
- ) );
复制代码- $taxonomies = get_terms( array(
- 'taxonomy' => 'taxonomy_name',
- 'hide_empty' => false
- ) );
-
- if ( !empty($taxonomies) ) :
- $output = '<select>';
- foreach( $taxonomies as $category ) {
- if( $category->parent == 0 ) {
- $output.= '<optgroup label="'. esc_attr( $category->name ) .'">';
- foreach( $taxonomies as $subcategory ) {
- if($subcategory->parent == $category->term_id) {
- $output.= '<option value="'. esc_attr( $subcategory->term_id ) .'">
- '. esc_html( $subcategory->name ) .'</option>';
- }
- }
- $output.='</optgroup>';
- }
- }
- $output.='</select>';
- echo $output;
- endif;
复制代码
分析class-wp-term-query.php
- var_dump($this->sql_clauses);
- var_dump($this->request);
- var_dump($args);
复制代码
- https://developer.wordpress.org/reference/functions/get_terms/
- http://wphun.com/f/get_terms
- https://www.ruikeedu.com/8569.html
|