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

rhy*_*lay 6 php wordpress woocommerce

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

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>'; 
Run Code Online (Sandbox Code Playgroud)

但是,当我在产品的短标题字段中输入值时,这不会覆盖标题.我想我需要一个与get_post_meta不同的功能

Jen*_*tel 6

你可以试试 :

global $product;
$product_name = $product->get_attribute( 'short-code' );
Run Code Online (Sandbox Code Playgroud)

代替

$product_name = get_post_meta($product->id, 'short-code', true)
Run Code Online (Sandbox Code Playgroud)

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