【问题标题】:get_terms(get_the_ID) shows WP_Errorget_terms(get_the_ID) 显示 WP_Error
【发布时间】:2019-03-28 17:25:08
【问题描述】:

我试图从帖子中显示我的自定义分类标签,但它返回 NULL 或 WP_Error

var_dump(get_terms(get_the_ID()));

// Returns this
object(WP_Error)#6268 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(20) "Taxonomia inválida." } } ["error_data"]=> array(0) { } }

为什么说它的分类法无效?

【问题讨论】:

  • 您需要通过分类,但您正在通过帖子 ID。试试 var_dump(get_terms('post_tag'));
  • 返回空数组
  • 因为你还没有添加任何帖子标签。尝试创建一些帖子标签并运行代码。

标签: wordpress tags taxonomy


【解决方案1】:

您使用了错误的函数,get_terms 将返回分类中的术语列表。如果要列出包含自定义分类的帖子的术语,则需要使用get_the_terms

var_dump( get_the_terms(get_the_ID(), 'your-custom-taxonomy');

https://developer.wordpress.org/reference/functions/get_the_terms/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2018-10-12
    • 1970-01-01
    • 2017-02-06
    • 2018-08-18
    相关资源
    最近更新 更多