WordPress函数get_categories用于获取分类的信息
get_categories( string|array $args = \'\' )
函数参数
$args
数组或字符串,get_categories支持的参数与WP_Term_Query::__construct()相同。
get_categories()函数默认参数值如下:
$args = array(\'taxonomy\' => \'category\',\'object_ids\' => null,\'orderby\' => \'name\',\'order\' => \'ASC\',\'hide_empty\' => true,\'include\' => array(),\'exclude\' => array(),\'exclude_tree\' => array(),\'number\' => \'\',\'offset\' => \'\',\'fields\' => \'all\',\'count\' => false,\'name\' => \'\',\'slug\' => \'\',\'term_taxonomy_id\' => \'\',\'hierarchical\' => true,\'search\' => \'\',\'name__like\' => \'\',\'description__like\' => \'\',\'pad_counts\' => false,\'get\' => \'\',\'child_of\' => 0,\'parent\' => \'\',\'childless\' => false,\'cache_domain\' => \'core\',\'update_term_meta_cache\' => true,\'meta_query\' => \'\',\'meta_key\' => \'\',\'meta_value\' => \'\',\'meta_type\' => \'\',\'meta_compare\' => \'\',);
可用值如下:
taxonomy
字符串或数组,默认值:category
指定分类法类型,默认为系统自带分类类型,仅在自行注册了分类的情况下使用。
object_ids
整数或数组
对应于wp_term_relationships表object_id字段,该字段对应于wp_posts表的ID,也就是指定文章的ID。
orderby
字符串
指定排序的类型,可选值如下:
- name:名称;
- slug:别名;
- term_group:类别组;
- term_id:类别ID;
- description:分类的描述;
- parent:父分类;
- meta_value:元数据的值。
order
字符串
排序的方式,可选值如下:
- DESC:降序;
- ASC:升序。
hide_empty
布尔值或整数,默认值:true
是否隐藏空分类,及隐藏没有文章的分类。
include
数组或字符串,如果为字符串值,多个值使用半角逗号分隔。
需要包含的分类ID
exclude
数组或字符串,如果为字符串值,多个值使用半角逗号分隔。
需要排除的分类ID
exclude_tree
数组或字符串,如果为字符串值,多个值使用半角逗号分隔。
排除分类的同时也会排除该分类下所有的子分类。
number
整数或字符串
返回结果的数量
offset
整数
偏移量
fields
字符串
要查询的字段
count
布尔值,默认false
返回分类包含的文章数量
name
字符串或数组
返回指定名称的分类
slug
字符串或数组
返回指定别名的分类
term_taxonomy_id
整数或数组
返回指定ID的分类
hierarchical
布尔值,默认值:true
是否按层级返回分类。
search
字符串
搜索匹配条件的分类
name__like
字符串
返回分类名称包含某个关键词的分类。
description__like
字符串
返回分类描述包含某个关键词的分类。
pad_counts
布尔值,默认值:false
是否将子标签的文章数量计算到count值中
get
字符串
child_of
整数
返回指定分类的子分类及子子分类。
parent
整数或字符串
返回指定分类的子分类。
childless
布尔值,默认值:false
返回没有子分类的分类
cache_domain
字符串,默认值:core
update_term_meta_cache
布尔值,默认值:true
meta_query
数组
元数据查询
meta_key
字符串
元数据键的名称
meta_value
字符串
元数据键的值
meta_type
字符串
meta_compare
字符串
meta_value的比较运算符
函数返回值
Array( [0] => WP_Term Object ( [term_id] => 6 [name] => 开发 [slug] => develop [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw [cat_ID] => 6 [category_count] => 0 [category_description] => [cat_name] => 开发 [category_nicename] => develop [category_parent] => 0 )......)
函数使用示例
$categories = get_categories( array( \'orderby\' => \'name\', \'parent\' => 0) ); foreach ( $categories as $category ) { printf( \'<a href=\"{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}1$s\">{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}2$s</a><br />\', esc_url( get_category_link( $category->term_id ) ), esc_html( $category->name ) );}
扩展阅读
get_categories()函数位于:wp-includes/class-wp-term-query.php
相关函数:
- get_terms()
- get_category()
不要再留垃圾评论了,主题君整理资源不容易,留几句鼓励的话吧。