【问题标题】:Uncaught Error: Cannot use object of type WP_Term as array未捕获的错误:无法将 WP_Term 类型的对象用作数组
【发布时间】:2017-06-05 19:12:39
【问题描述】:

我在更新 WordPress 后遇到了这个问题,在我的网站上我有一个自定义帖子,其中包含一些自定义类别,类似这样:

1) 父类:食品 | 儿童:薯条、汉堡包、枫糖浆……

2) 父类别:年份 | 儿童:2016、2015、2014…

3) 父类别:国家 | 儿童:美国、加拿大、西班牙……

因此,当我撰写自定义帖子时,我会在这些类别中进行选择,然后选择(勾选框)我需要的类别。它会显示类似的内容:

标题:新配方

内容:我的文字

条款食物:枫糖浆 / 国家:加拿大 / 年份:2014

但是现在,条款根本没有显示,我收到以下错误消息: 不能将 WP_Term 类型的对象用作数组

我曾经有以下 PHP 代码,它允许我检索子类别的父类别(并将其用作前缀),还允许我更改顺序。

  $term_list = wp_get_post_terms($post->ID, 'project_cat', array("fields" => "all"));
                            $terms_hierarchy = array();
                            foreach ($term_list as $term_single) {
                                $parent = $term_single->parent;
                                if ($parent != 0) {
                                    $terms_hierarchy[$parent][] = get_term($parent)->slug;
                                    $terms_hierarchy[$parent]['children'][$term_single->term_id] = $term_single->name;
                                } else {
                                    $terms_hierarchy[$parent] = $term_single;
                                }
                            }
   //PHP indicated this line:
                            foreach ($terms_hierarchy as $key => $term) {
                                echo "<span>$term[0]: </span>";
                                if (!empty($term['children'])) {
                                    $s_children = '';
                                    foreach ($term['children'] as $key => $child) {
                                        if ($term[0] == 'client') {
                                            $tax_meta = get_term_meta($key);
                                            if(!empty($tax_meta['external_url'][0])){
                                               $s_children .= "<a target='_blank' href='{$tax_meta['external_url'][0]}'>$child</a>, ";
                                            }
                                            else {
                                                $s_children .= $child . ', ';
                                            }
                                        }
                                        else {
                                            $s_children .= $child . ', ';
                                        }
                                    }
                                    echo rtrim($s_children, ', ') . "<br />";
                                }
                            }

如果有人能帮助找出问题所在,我将不胜感激?

感谢您的宝贵时间

【问题讨论】:

  • 抱歉再次询问,但到目前为止我无法自行解决问题...目前,我的临时解决方案是删除该行,但随后,我的大部分类别都会消失...会非常感谢一些提示!

标签: php arrays wordpress taxonomy-terms


【解决方案1】:

$terms_hierarchy 不是一个数组,它是一个标准对象。所以...

$term['孩子']

...实际上是...

$term->孩子

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2017-11-12
  • 1970-01-01
  • 2023-04-03
  • 2018-07-07
  • 2016-12-17
  • 1970-01-01
  • 2017-12-08
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多