在 WooCommerce 存档页面中的产品标题下方添加自定义字段值
Add a custom field value below product title in WooCommerce archives pages
在 WooCommerce 中,我想将自定义文本添加到我的产品显示中,该文本将从产品编辑页面的自定义字段中获取。
现在是这样的:
您可以在下面看到带有标题的产品:
我想在每个产品下面添加一条文字,用蓝笔标记:
我设法找到了一些自定义 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));
}
但我不知道如何将自定义字段连接到产品的显示,
因此自定义字段的文本将显示在产品标题下方。
如何在产品标题下方显示自定义字段
更新:
试试这个自定义挂钩函数,它将在产品标题下方显示您的自定义字段值:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
应该可以。
对于尝试实现此功能的任何人,函数 woocommerce_product_custom_fields() 中缺少结尾 div。添加 echo '';在右大括号之前,否则产品设置中的所有其他产品数据选项卡都将为空。
如果您尝试使用此代码,结束 div 需要是:
echo '</div>';
否则 echo '<div class="product_custom_field">';
保持打开状态。
在 WooCommerce 中,我想将自定义文本添加到我的产品显示中,该文本将从产品编辑页面的自定义字段中获取。
现在是这样的:
您可以在下面看到带有标题的产品:
我想在每个产品下面添加一条文字,用蓝笔标记:
我设法找到了一些自定义 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));
}
但我不知道如何将自定义字段连接到产品的显示, 因此自定义字段的文本将显示在产品标题下方。
如何在产品标题下方显示自定义字段
更新:
试试这个自定义挂钩函数,它将在产品标题下方显示您的自定义字段值:
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
global $product;
// Get the custom field value
$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );
// Display
if( ! empty($custom_field) ){
echo '<p class="my-custom-field">'.$custom_field.'</p>';
}
}
代码进入您的活动 child 主题(或活动主题)的 function.php 文件。
应该可以。
对于尝试实现此功能的任何人,函数 woocommerce_product_custom_fields() 中缺少结尾 div。添加 echo '';在右大括号之前,否则产品设置中的所有其他产品数据选项卡都将为空。
如果您尝试使用此代码,结束 div 需要是:
echo '</div>';
否则 echo '<div class="product_custom_field">';
保持打开状态。