wordpress - Wordpress 将 woocommerce 类别作为数组获取
问题描述
我需要将所有类别放在一个数组中以自动将产品添加到类别中,但我失败了:/我有这个代码:
function getCats($catlist, $name) {
$regex = '('.implode('|', $catlist).')';
$success = preg_match_all($regex, $name, $matches);
return $success ? $matches[0] : [];
}
在其他功能中:
$catlist = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
$firstCat = getCats($catlist, $name)[0];
但如果我运行代码,我会收到以下消息:
WP_Term
无法将类的对象转换为字符串
有谁知道我如何处理它以将所有类别保存在一个字符串中?
解决方案
我检查过get_terms()
函数返回错误:
WP_Error 对象([错误] => 数组([invalid_taxonomy] => 数组([0] => 无效分类。))[error_data] => 数组())
$catlist = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
这就是它给出错误的原因:
WP_Term 类的对象无法转换为字符串
注意:请先Invalid taxonomy
使用https://wordpress.stackexchange.com/questions/13480/get-terms-return-errors解决错误,然后它会自动解决。
推荐阅读
- html - 位置绝对元素未显示
- python - 为什么我的傅立叶变换不能正常工作?
- python - 将 VTK 图像 (.vti) 数据转换为 VTK 多边形 (.vtp) 数据
- react-native - expo-contacts 不会在反应原生 Expo 37 中过滤带有字段选项的联系人
- java - 为什么使用 BouncyCastle 解密的文本与输入文本有点不同?
- python - 您如何对光谱数据执行 PCA?
- html - 如何检查没有文件通过表单上传到服务器?
- flutter - 从应用程序(使用颤振)向 ESP8266 发送数据时,我在 arduino 中收到一条乱码消息
- c# - 如何在 C# 中以 CQRS 模式对命令处理程序进行单元测试
- javascript - 显示图像 - Popup ArcGIS API for Javascript