在wordpress建站中,对分类category的数据进行获取的应用存在于网站各处,如分类的菜单、面包屑导航、文章中所属分类的信息等等,因此需要对category分类的一些获取方法有所了解,官方给出了很多默认的方法,这里只列出一些基础常用的方法及说明。
记录:检索检索给定 ID 类别的数据
检索给定类别 ID 或类别对象的类别数据。
如果向 $category 参数传递一个对象,则假定该对象是从数据库中检索到的类别行对象。它将缓存类别数据。
如果向 $category 传递类别 ID 的整数,则将从数据库中检索该类别(如果尚未缓存)并将其传回。
语法:get_category( int|object $category , string $output = OBJECT , string $filter = ‘raw’ )
参数:
- $category ( int | object ) (必需) 类别 ID 或类别行对象。
- $output ( string ) (可选) 所需的返回类型。OBJECT、ARRAY_A 或 ARRAY_N 之一,分别对应于WP_Term对象、关联数组或数值数组。默认值:对象
- $filter ( string ) (可选) 如何清理类别字段。默认值:“row”
返回:
(object|array| WP_Error |null) $output 参数定义的类型中的类别数据。 如果 $category 为空,则为 WP_Error,如果不存在则为 null。
示例:
<?php $category = get_category( 106 ); ?> 的输出print_r( $category );
将得到:
stdClass Object(
[term_id] => 106 // ID
[name] => Category Name // 分类名称
[slug] => category-name // 分类别名
[term_group] => 0
[term_taxonomy_id] => 108 // 分类法ID
[taxonomy] => category // 分类法名称
[description] => This is the category description. // 分类描述
[parent] => 62 // 分类父级ID
[count] => 17
[filter] => raw
[cat_ID] => 106
[category_count] => 17
[category_description] => This is the category description.
[cat_name] => Category Name
[category_nicename] => category-name
[category_parent] => 62
)
记录:获取分类的信息列表
检索类别对象的列表。