WooCommerce - 检查属性是否存在然后给变量属性值

WooCommerce - check if attribute exists and then give variable attribute value

我用 slug 'short-title' 为我的 woocommerce 产品创建了一个新属性。这个想法是我想用短标题覆盖商店页面上的标题。我已经用以下内容设置了一个循环:

if (get_post_meta($product->id, 'short-code', true)) {
$product_name =  (get_post_meta($product->id, 'short-code', true));
} else {
$product_name =  get_the_title();
}
echo '<div class="product-below"><h4 class="product-name">'.$product_name.'</h4>'; 

但是当我在产品的短标题字段中输入值时,这并没有覆盖标题。我想我需要一个不同于 get_post_meta

的函数

你可以试试:

global $product;
$product_name = $product->get_attribute( 'short-code' );

而不是

$product_name = get_post_meta($product->id, 'short-code', true)

如果它不适合你,那么也请确认添加属性的 slug 是 short-codeshort_code