正如标题所说-我有一个single.php模板,它获取当前分配的帖子的类别。
$cat_post = get_the_category();
该函数返回分配给帖子的类别。从4.0.24升级到4.9.7后,它停止工作。我唯一想到的就是这个函数不受支持,但我还没有发现这方面的一些东西。
有什么想法吗?
发布于 2018-07-17 03:39:37
你可以使用这个
//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
发布于 2021-02-16 23:08:54
我认为您正在尝试从当前帖子中获取一个类别,即自定义帖子类型。根据以下函数的文档,它不适用于自定义post类型。你可以在下面的链接上看到它。Click here to see the Code Reference
对于自定义的post类型,可以使用get_the_terms()方法。下面是函数的代码引用的link。
https://stackoverflow.com/questions/51364797
复制相似问题