[英]Is there a shortcode for product description in WooCommerce
是否有任何短代碼來調用產品描述(標題下的文本字段)?
現在,我正在使用另一個自定義字段來完成這項工作,但如果我使用 WooCommerce 字段會更好。
您可以通過以下方式構建自己的短代碼:
add_shortcode( 'product_description', 'display_product_description' );
function display_product_description( $atts ){
$atts = shortcode_atts( array(
'id' => get_the_id(),
), $atts, 'product_description' );
global $product;
if ( ! is_a( $product, 'WC_Product') )
$product = wc_get_product($atts['id']);
return $product->get_description();
}
代碼位於活動子主題(活動主題)的 function.php 文件中。 測試和工作。
用法示例[product_description]
1) 在當前產品頁面 ph:
echo do_shortcode( "[product_description]" );
2) 在任何提供相關產品 ID 的 php 代碼中
echo do_shortcode( "[product_description id='37']" );
我寫了一個與@LoicTheAztec 上面的答案非常相似的解決方案,但更具防御性(因為他的解決方案破壞了 Elementor 對我的編輯,因為它在保存時執行短代碼時沒有產品上下文)。
它還可以解決您的段落/換行問題,因為內容在返回之前已格式化(基本上用<p>
標簽替換換行符)。
function custom_product_description($atts){
global $product;
try {
if( is_a($product, 'WC_Product') ) {
return wc_format_content( $product->get_description("shortcode") );
}
return "Product description shortcode run outside of product context";
} catch (Exception $e) {
return "Product description shortcode encountered an exception";
}
}
add_shortcode( 'custom_product_description', 'custom_product_description' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.