Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在单个产品页面上的价格后添加自定义元字段值_Php_Wordpress_Woocommerce_Product_Meta Boxes - Fatal编程技术网

Php 在单个产品页面上的价格后添加自定义元字段值

Php 在单个产品页面上的价格后添加自定义元字段值,php,wordpress,woocommerce,product,meta-boxes,Php,Wordpress,Woocommerce,Product,Meta Boxes,我已经在我的产品页面上创建了一个名为“Info”的自定义元数据库。 我想在产品价格后面显示模板content single product.php中相应的元字段值。但是它没有输出相应的元字段值,这是不起作用的 我的问题是,我不知道如何调用相应的元字段值。它是在function.phpphp文件中还是在content single product.php模板中 这是我用来创建Metabox的代码(它可以正常工作): 我做错了什么,我怎样才能让它工作 谢谢 更新 1) 您在上一个函数中未获得产品I

我已经在我的产品页面上创建了一个名为“Info”的自定义元数据库。
我想在产品价格后面显示模板content single product.php中相应的元字段值。但是它没有输出相应的元字段值,这是不起作用的

我的问题是,我不知道如何调用相应的元字段值。它是在
function.php
php文件中还是在
content single product.php
模板中

这是我用来创建Metabox的代码(它可以正常工作):

我做错了什么,我怎样才能让它工作

谢谢

更新

1) 您在上一个函数中未获得产品ID。为此,您需要使用WordPress
获取ID()
,数据值将根据需要输出。
2) 您的函数
new\u meta\u save()
与相应的添加操作(hook)中的函数名称不同。
3) 所有的代码都在活动主题中的
function.php
文件中运行。

以下是您重新访问的功能代码:

// Creating a custom metabow in Admin Individual product pages
add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
    add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}


function info_meta_fields_output($post)
{
    $new_meta = get_post_meta($post->ID,'_new_meta',true);
    echo ('<label for="new_meta"> info meta box</label>');
    echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}

add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
    $new_meta=sanitize_text_field($_POST['new_meta']);
    update_post_meta ($post_id,'_new_meta',$new_meta);
}


// Displaying the value on single product pages
function meta_product($product_id) {

    $new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
    echo $new_meta2;
}
add_action('woocommerce_single_product_summary', 'meta_product',15);

谢谢LoicTheAztec,但我的问题是metabox并没有显示在产品中。我知道这件事的重要性woocommerce@user142146我已经更新了我的答案。现在一切都如您所期望的那样工作(我已经完全测试了它)。对不起,你的问题不是很清楚;所以首先我不明白你的问题是什么。谢谢你,谢谢你。
// add metabox to behind price product

function meta_product() {
    // don't show in product --problem
    $new_meta2 = get_post_meta($post->ID,'_new_meta',true);
    echo $new_meta2;

    // show in product
    echo "123456";
}
add_action('woocommerce_single_product_summary', 'meta_product',25);
// Creating a custom metabow in Admin Individual product pages
add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
    add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}


function info_meta_fields_output($post)
{
    $new_meta = get_post_meta($post->ID,'_new_meta',true);
    echo ('<label for="new_meta"> info meta box</label>');
    echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}

add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
    $new_meta=sanitize_text_field($_POST['new_meta']);
    update_post_meta ($post_id,'_new_meta',$new_meta);
}


// Displaying the value on single product pages
function meta_product($product_id) {

    $new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
    echo $new_meta2;
}
add_action('woocommerce_single_product_summary', 'meta_product',15);
    <?php
        /**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         */
        do_action( 'woocommerce_single_product_summary' );
    ?>