WC_Product
get_attribute()
add_filter( 'woocommerce_after_shop_loop_item_title', 'loop_display_ingredients', 15 );
function loop_display_ingredients() {
global $product;
// The attribute slug
$attribute = 'ingredients';
// Get attribute term names in a coma separated string
$term_names = $product->get_attribute( $attribute );
// Display a coma separted string of term names
echo '<p>' . $term_names . '</p>';
}
现在如果你想的话
在昏迷分隔列表中,您将使用以下内容:
// The attribute slug
$attribute = 'ingredients';
// Get attribute term names in a coma separated string
$term_names = $product->get_attribute( $attribute );
// Get the array of the WP_Term objects
$term_slugs = array();
$term_names = str_replace(', ', ',', $term_names);
$term_names_array = explode(',', $term_names);
if(reset($term_names_array)){
foreach( $term_names_array as $term_name ){
// Get the WP_Term object for each term name
$term = get_term_by( 'name', $term_name, 'pa_'.$attribute );
// Set the term slug in an array
$term_slugs[] = $term->slug;
}
// Display a coma separted string of term slugs
echo '<p>' . implode(', ', $term_slugs); . '</p>';
}