找回密码
 免费注册

常用函数-get_terms()

[复制链接]
admin 发表于 2020-9-12 10:54:58 | 显示全部楼层 |阅读模式
经过三小时的源码分析,wordpress在处理递归层级结构时也是先将所有结果查询出来,然后用PHP处理

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

用法 get_terms($taxonomies, $args = )   有效变量

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

  1. $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

  1. $terms = get_terms( 'post_tag', array(
  2.     'hide_empty' => false,
  3. ) );

  4. $terms = get_terms( array(
  5.     'taxonomy' => 'post_tag',
  6.     'hide_empty' => false,
  7. ) );
复制代码
  1. $taxonomies = get_terms( array(
  2.     'taxonomy' => 'taxonomy_name',
  3.     'hide_empty' => false
  4. ) );

  5. if ( !empty($taxonomies) ) :
  6.     $output = '<select>';
  7.     foreach( $taxonomies as $category ) {
  8.         if( $category->parent == 0 ) {
  9.             $output.= '<optgroup label="'. esc_attr( $category->name ) .'">';
  10.             foreach( $taxonomies as $subcategory ) {
  11.                 if($subcategory->parent == $category->term_id) {
  12.                 $output.= '<option value="'. esc_attr( $subcategory->term_id ) .'">
  13.                     '. esc_html( $subcategory->name ) .'</option>';
  14.                 }
  15.             }
  16.             $output.='</optgroup>';
  17.         }
  18.     }
  19.     $output.='</select>';
  20.     echo $output;
  21. endif;
复制代码

分析class-wp-term-query.php
  1. var_dump($this->sql_clauses);
  2. var_dump($this->request);
  3. var_dump($args);
复制代码


  • https://developer.wordpress.org/reference/functions/get_terms/
  • http://wphun.com/f/get_terms
  • https://www.ruikeedu.com/8569.html

回复

使用道具 举报

 楼主| admin 发表于 2020-9-12 11:00:38 | 显示全部楼层
代码位于 wp-includes/taxonomy.php
  • is_taxonomy_hierarchical() 判断一个分类法是否有分级
  • get_taxonomy()通过分类法名称获取一个分类法的信息
  • get_term_children
  • get_term
  • _get_term_hierarchy
  • _get_term_children

  1. function is_taxonomy_hierarchical( $taxonomy ) {
  2.         if ( ! taxonomy_exists( $taxonomy ) ) {
  3.                 return false;
  4.         }

  5.         $taxonomy = get_taxonomy( $taxonomy );
  6.         return $taxonomy->hierarchical;
  7. }
复制代码
  1. function _get_term_hierarchy( $taxonomy ) {
  2.         if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
  3.                 return array();
  4.         }
  5.         $children = get_option( "{$taxonomy}_children" );

  6.         if ( is_array( $children ) ) {
  7.                 return $children;
  8.         }
  9.         $children = array();
  10.         $terms    = get_terms(
  11.                 array(
  12.                         'taxonomy'               => $taxonomy,
  13.                         'get'                    => 'all',
  14.                         'orderby'                => 'id',
  15.                         'fields'                 => 'id=>parent',
  16.                         'update_term_meta_cache' => false,
  17.                 )
  18.         );
  19.         foreach ( $terms as $term_id => $parent ) {
  20.                 if ( $parent > 0 ) {
  21.                         $children[ $parent ][] = $term_id;
  22.                 }
  23.         }
  24.         update_option( "{$taxonomy}_children", $children );

  25.         return $children;
  26. }
复制代码

  • http://wphun.com/f/is_taxonomy_hierarchical
  • http://wphun.com/f/get_taxonomy


回复

使用道具 举报

 楼主| admin 发表于 2020-9-12 15:26:13 | 显示全部楼层
  1. if ( $hierarchical && $number && is_array( $terms ) ) {
  2.     if ( $offset >= count( $terms ) ) {
  3.         $terms = array();
  4.     } else {
  5.         $terms = array_slice( $terms, $offset, $number, true );
  6.     }
  7. }
复制代码

分级处理后返回偏移的数据
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|Archiver|手机版|小黑屋|信息共享网

GMT+8, 2024-5-15 22:24 , Processed in 0.083540 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表