在 WooCommerce 档案页面中的产品标题下方添加自定义字段值 [英] Add a custom field value below product title in WooCommerce archives pages

查看:25
本文介绍了在 WooCommerce 档案页面中的产品标题下方添加自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我想将自定义文本添加到我的产品显示中,该文本将从产品编辑页面的自定义字段中获取.

现在的样子:

您可以在下面看到带有标题的产品:

我想在每个产品下方添加一段文字,用蓝色钢笔标记:

我设法找到了一些自定义 php 代码来在产品页面中添加自定义字段,如下所示:

.

这是我的代码:

//显示字段add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');//保存字段add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');函数 woocommerce_product_custom_fields(){全球 $woocommerce, $post;echo '

';//自定义产品文本字段woocommerce_wp_text_input(大批('id' =>'_custom_product_text_field','占位符' =>'自定义产品文本字段','标签' =>__('自定义产品文本字段', 'woocommerce'),'desc_tip' =>'真的'));}函数 woocommerce_product_custom_fields_save($post_id){//自定义产品文本字段$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];如果(!空($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 );函数 custom_field_display_below_title(){全球$产品;//获取自定义字段值$custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );//展示如果(!空($custom_field)){echo '<p class="my-custom-field">'.$custom_field.'</p>';}}

代码位于活动子主题(或活动主题)的 function.php 文件中.

它应该可以工作.

In WooCommerce I would like to add custom text to my products display, that will be grabbed from a custom field in product's edit page.

This is how it looks now:

You can see the products with their title below:

I would like to add a text below every product, marked with blue pen:

I have managed to find some custom php code to add custom field in product's page like this:

.

This is my code:

    // 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));
}

But I don't know how to connect the custom field to the product's display, so the custom field's text will be shown below the product's title.

How to display the custom field below product title

解决方案

Updated:

Try this custom hooked function, that will display your custom field value below product title:

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>';
    }
}

Code goes in function.php file of your active child theme (or active theme).

It should work.

这篇关于在 WooCommerce 档案页面中的产品标题下方添加自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆