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

时间:2015-03-10 03:24:19

标签: php wordpress woocommerce

我使用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不同的函数

1 个答案:

答案 0 :(得分:6)

您可以尝试:

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

而不是

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

如果它对您不起作用,请同时确认slug的添加属性为short-codeshort_code