我使用以下功能在面包屑中添加 WooCommerce 父类别和子类别:
// Load Woocommerce product parent & sub-categories in breadcrumbs
add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
function post_parent_trail ($crumbs, $args) {
if ( is_product() || is_shop() ) {
global $product;
$category = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
if ( count( $category ) > 1 ) {
$cat_array = array();
$cat_array[1]['label'] = $category[1]->name;
$cat_array[1]['url'] = get_term_link( $category[1]->term_id );
$cat_array[2]['label'] = $category[0]->name;
$cat_array[2]['url'] = get_term_link( $category[0]->term_id );
array_splice($crumbs, 2, 0, $cat_array);
}
}
return $crumbs;
}
但收到此错误:
[18-Jul-2021 11:58:36 UTC] PHP 注意:试图在 /home/user/public_html/site/wp-content/themes/pro-child/functions.php 中获取非对象的属性“名称”在第 969 行 [2021 年 7 月 18 日 11:58:36 UTC] PHP 通知:尝试在 /home/user/public_html/site/wp-content/themes/pro-child/ 中获取非对象的属性“term_id”第 970 行的 functions.php [18-Jul-2021 11:58:36 UTC] PHP 注意:试图在 /home/user/public_html/site/wp-content/themes/pro 中获取非对象的属性“名称” -child/functions.php 在第 971 行 [18-Jul-2021 11:58:36 UTC] PHP 注意:试图在 /home/user/public_html/site/wp-content/ 中获取非对象的属性“term_id” theme/pro-child/functions.php 第 972 行 [2021 年 7 月 18 日 11:58:36 UTC] PHP 致命错误:未捕获的错误:类 WP_Error 的对象无法转换为 /home/user/public_html/site/wp-includes/formatting.php:1098 中的字符串
你能指导我解决这个问题吗?