get_the_category()wordpress函数在更新核心版本后停止工作



正如标题所说,我有一个单独的.php模板,它可以获得当前帖子的类别。

$cat_post = get_the_category();

该函数返回分配给职位的类别。从4.0.24升级到4.9.7后,它停止了工作。我唯一想到的是该功能不受支持,但我还没有找到相关信息。

有什么想法吗?

您可以使用此

//get category 
$category   = get_the_category(get_the_ID());
//the above code will return an Array
Array
(
[0] => WP_Term Object
(
[term_id] => 2
[name] => video
[slug] => video
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] => 
[parent] => 0
[count] => 1
[filter] => raw
[cat_ID] => 2
[category_count] => 1
[category_description] => 
[cat_name] => video
[category_nicename] => video
[category_parent] => 0
)
)
// you can access this array like this
$catID = $category[0]->term_id;
//for term id
$catName = $category[0]->name;
//for category name

我认为您正在尝试从当前帖子中获取一个类别,即自定义帖子类型。根据以下函数的文档,它不适用于自定义帖子类型。你可以在下面的链接上看到它。单击此处查看代码参考

对于自定义帖子类型,可以使用get_the_terms((方法。这是函数的代码参考的链接。

最新更新