【发布时间】:2020-12-04 02:15:40
【问题描述】:
我想在后端 Woocommerce 产品页面中添加一个文本字段,并在产品标题下方的前端显示/回显文本。
现在我有“自定义字段框”来在后端编写文本(见截图),但我不知道如何在前端显示文本。有人可以帮我处理这段代码吗?
我关注了这个页面,但它只适用于存档页面...... Add a custom field value below product title in WooCommerce archives pages
提前谢谢你!
杰瑞
函数.php
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce'),
'desc_tip' => 'true'
)
);
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}
【问题讨论】:
-
我认为这里的问题是选择正确的挂钩位置。
-
请对以下答案提供一些反馈,我们将不胜感激。谢谢。
标签: php wordpress woocommerce product hook-woocommerce