Add a custom field value below product title in WooCommerce archives pages

前端 未结 2 590
夕颜
夕颜 2021-01-01 04:12

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:

相关标签:
2条回答
  • 2021-01-01 04:35

    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.

    0 讨论(0)
  • 2021-01-01 04:49

    For anyone trying to implement this, the closing div is missing from function woocommerce_product_custom_fields(). Add echo ''; before the closing curly bracket otherwise all of the other product data tabs in the product settings will be empty.

    0 讨论(0)
提交回复
热议问题