WordPress获取分类对象函数:get_category

文 / @WordPress主题

WordPress函数get_category用于获取分类对象。

get_category( int|object $category, string $output=OBJECT, string $filter='raw' )

函数参数

$category

整数或对象

分类的ID或分类对象

$output

字符串,默认值:OBJECT

返回的数据类型,默认返回对象,可选值:

  • OBJECT:默认值,以对象的形式返回数据;
  • ARRAY_A:以关联数组的形式返回数据;
  • ARRAY_N:以数字数组的形式返回数据。

$filter

字符串,默认值:raw

对分类使用函数sanitize_term进行字段过滤

函数返回值

WP_Term Object
(
    [term_id] => 1
    [name] => 分类标题
    [slug] => slug
    [term_group] => 0
    [term_taxonomy_id] => 1
    [taxonomy] => category
    [description] => 分类描述
    [parent] => 0
    [count] => 22
    [filter] => raw
    [cat_ID] => 1
    [category_count] => 22
    [category_description] => 分类描述
    [cat_name] => 分类标题
    [category_nicename] => slug
    [category_parent] => 0
)

函数使用示例

$category = get_category(2);
echo '<a href="' . get_category_link($category->cat_ID) . '">' . $category->name . '</a>';

扩展阅读

get_category()函数位于:wp-includes/category.php

相关函数:

  • get_term()
  • get_tag()
  • get_category_children()
添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。