Rém*_*émi 1 php wordpress shortcode custom-taxonomy woocommerce
我是 WooCommerce 的新手,正在寻找一种在帖子页面上显示产品属性的解决方案。我进行了研究和一些测试,但似乎没有任何效果。
理想情况下,我想使用短代码获取产品 ID并在我的帖子页面上显示他的所有产品属性。就像是[product_page id="99" display_only_attributes ]
这是在自定义简码中获取产品属性的方法,您将在其中将产品 ID 定义为简码参数id
。
功能代码:
if ( ! function_exists( 'display_product_additional_information' ) ) {
function display_product_additional_information($atts) {
// Shortcode attribute (or argument)
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_additional_information' );
// If the "id" argument is not defined, we try to get the post Id
if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
$atts['id'] = get_the_id();
}
// We check that the "id" argument is a product id
if ( get_post_type($atts['id']) === 'product' ) {
$product = wc_get_product($atts['id']);
}
// If not we exit
else {
return;
}
ob_start(); // Start buffering
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean(); // Return the buffered outpout
}
add_shortcode('product_additional_information', 'display_product_additional_information');
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
短代码用法 (更新)
具有定义的产品 ID:
[product_additional_information id='37']
Run Code Online (Sandbox Code Playgroud)
或者在 php 中:
echo do_shortcode("[product_additional_information id='37']");
Run Code Online (Sandbox Code Playgroud)在现有产品页面中(例如,当“附加信息”产品选项卡被删除时):
[product_additional_information]
Run Code Online (Sandbox Code Playgroud)
或者在 php 中:
echo do_shortcode("[product_additional_information]");
Run Code Online (Sandbox Code Playgroud)你会得到这样的东西:
归档时间: |
|
查看次数: |
4640 次 |
最近记录: |