【发布时间】:2018-07-08 03:12:54
【问题描述】:
在 woocommerce 中,我使用一些代码在产品编辑页面中添加带有自定义字段的 Metabox。
如何在单个产品页面的简短描述下显示此自定义字段的值?
这是我的代码:
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"> Custom Text </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 ('<p id="value-on-single-product">' . $new_meta2 . '</p>');
}
add_action('woocommerce_single_product_summary', 'meta_product',30);
但它不显示自定义字段值。
【问题讨论】:
-
您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples。
标签: php wordpress woocommerce custom-fields meta-boxes