WordPress根据分类路径获取分类对象函数:get_category_by_path

WordPress函数get_category_by_path可以根据分类的路径或别名获取分类对象。

1
get_category_by_path( string $category_path, bool $full_match=true, string $output=OBJECT )

函数参数

$category_path

字符串

包含分类别名的路径或者直接提供别名

$full_match

布尔值,默认值:true

如果$category_path的值为完整的URL时,$full_match的值需要为false

$output

字符串,默认值:OBJECT

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

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

函数返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
WP_Term Object
(
    [term_id] => 1
    [name] => 分类名称
    [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] => 分类别名
    [category_parent] => 0
)

函数使用示例

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

扩展阅读

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

相关函数:

  • get_term()
  • get_category_children()
  • get_category_by_slug()
原文链接:https://xiaohost.com/2292.html,转载请注明出处。
0