试图显示我在产品下注册的自定义分类法“w_label”。但是,当我尝试使用以下代码显示它时:
register_taxonomy('w_label', array('product'),
array(
'hierarchical' => true,
'label' => 'Product Labels',
'singular_label' => 'Product Label',
'rewrite' => true,
'supports' => array('excerpt', 'thumbnail')
)
);
function w_label_name () {
global $post;
$terms = get_the_terms( $post->ID, 'w_label' );
foreach ( $terms as $term ){
echo '<div class="label">' . $term->name . '</div>';
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'w_label_name', 2 );
我不断收到“警告:为 foreach() 提供的参数无效”
不知道我错过了什么。如果我将此代码用于默认的 WooCommerce 类别,它可以工作,但不适用于我在此处注册的自定义分类法。