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

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

问题描述

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

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.

这是现在的样子:

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

You can see the products with their title below:

我想在每个产品下方添加一个用蓝色笔标记的文本:

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

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

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

.

这是我的代码:

    // 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

推荐答案

已更新:

尝试使用此自定义挂钩函数,该函数将在产品标题下方显示您的自定义字段值:

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

代码进入您的活动子主题(或活动主题)的function.php文件中.

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

应该可以.

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

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