[我正在尝试找到一种方法,可以通过wordpress自定义字段将自定义html内容添加到woocommerce中可钩的任何位置。
我在下面有一些代码,但是不起作用?
我为要在所选的woocommerce产品页面上使用的可挂钩空间创建了一个新的Wordpress自定义字段:
自定义字段名称:before_single_product_content
自定义字段值:您的自定义HTML标记。
我添加了我的孩子主题的functions.php:
/** WooCommerce woocommerce_before_single_product - content **/
function before_single_product() {
global $post;
$product_id = $post->ID;
$BeforeSingleProductContentValue = get_post_meta($product_id,'before_single_product_content',true);
if(!$BeforeSingleProductContentValue) return;
echo ''.$BeforeSingleProductContentValue.'';
}
add_action('woocommerce_before_single_product','before_single_product');
单个产品后的重复,例如:
/** WooCommerce woocommerce_after_single_product - content **/
function after_single_product() {
global $post;
$product_id = $post->ID;
$AfterSingleProductContentValue = get_post_meta($product_id,'after_single_product_content',true);
if(!$AfterSingleProductContentValue) return;
echo ''.$AfterSingleProductContentValue.'';
}
add_action('woocommerce_after_single_product','after_single_product');
欢迎提供任何提示!
尝试这种方式
/** WooCommerce woocommerce_before_single_product - content **/
function before_single_product() {
global $product;
// Get product id
$product_id = $product->get_id();
$BeforeSingleProductContentValue = get_post_meta( $product_id, 'before_single_product_content', true );
if ( isset ($BeforeSingleProductContentValue) {
echo $BeforeSingleProductContentValue;
}
}
add_action('woocommerce_before_single_product','before_single_product');