首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >get_the_category() wordpress函数在更新核心版本后停止工作

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

Stack Overflow用户
提问于 2018-07-16 23:01:39
回答 2查看 1.7K关注 0票数 0

正如标题所说-我有一个single.php模板,它获取当前分配的帖子的类别。

代码语言:javascript
复制
$cat_post = get_the_category();

该函数返回分配给帖子的类别。从4.0.24升级到4.9.7后,它停止工作。我唯一想到的就是这个函数不受支持,但我还没有发现这方面的一些东西。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2018-07-17 03:39:37

你可以使用这个

代码语言:javascript
复制
//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
票数 1
EN

Stack Overflow用户

发布于 2021-02-16 23:08:54

我认为您正在尝试从当前帖子中获取一个类别,即自定义帖子类型。根据以下函数的文档,它不适用于自定义post类型。你可以在下面的链接上看到它。Click here to see the Code Reference

对于自定义的post类型,可以使用get_the_terms()方法。下面是函数的代码引用的link

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51364797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档