【问题标题】:WooCommerce | Add product subheading underneath product title on archive pageWooCommerce |在存档页面的产品标题下方添加产品副标题
【发布时间】:2019-08-11 22:23:10
【问题描述】:

在 WooCommerce 档案页面上,我想在每个产品的标题下方添加一个副标题。

See this image for reference. 红色框表示子标题位置。

我尝试关注这个人 Add a custom field value below product title in WooCommerce archives pages ,但我无法使用自定义产品文本字段。

我的 Wordpress 主题 OceanWP 有一个 subheading field.

我试图从上面链接的帖子中更改代码,并想出了这个,但它不起作用:

add_action( 'woocommerce_after_shop_loop_item_title', 'subheading_display_below_title', 2 );
function subheading_display_below_title(){
    global $product;

    // Get the custom field value
    $subheading = get_post_meta( $product->get_id(), '_subheading', true );

    // Display
    if( ! empty($subheading) ){
        echo '<p class="subheading">'.$subheading.'</p>';
    }
}

如何在产品存档页面的产品标题下方添加我的 OceanWP 产品副标题?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    要使您的代码正常工作,您首先需要为产品添加副标题自定义字段。为此,您可以这样做:

    //Register the product custom field
    add_action( 'woocommerce_product_options_general_product_data', 'my_woocommerce_product_subheading' );
    
    function my_woocommerce_product_subheading() {
        $args = array(
            'label' => 'Subheading', // Text in Label
            'placeholder' => 'My Subheading',
            'id' => 'product_subheading', // required
            'name' => 'product_subheading',
            'desc_tip' => 'The product subheading',
        );
        woocommerce_wp_text_input( $args );
    }
    
    //Save the custom field as product custom post meta
    add_action( 'woocommerce_process_product_meta', 'my_woocommerce_save_product_subheading' );
    
    function my_woocommerce_save_product_subheading( $post_id ) {
        $product = wc_get_product( $post_id );
        $title = isset( $_POST['product_subheading'] ) ? $_POST['product_subheading'] : '';
        $product->update_meta_data( 'product_subheading', sanitize_text_field( $title ) );
        $product->save();
    }
    
    //Display the custom field on product page loop below the title
    add_action( 'woocommerce_after_shop_loop_item_title', 'subheading_display_below_title', 2 );
    function subheading_display_below_title(){
        global $product;
    
        // Get the custom field value
        $subheading = get_post_meta( $product->get_id(), 'product_subheading', true );
    
        // Display
        if( ! empty($subheading) ){
            echo '<p class="subheading">'.$subheading.'</p>';
        }
    }
    
    
    

    【讨论】:

    • 如何指定产品副标题?当我使用此代码时,数字,而不是副标题,会出现在产品图片的上方,而不是产品标题的下方。 See this image for the result
    • 要添加副标题,编辑您的产品,在“常规”选项卡下,您应该会看到副标题字段See example。我已经修复了上面的代码(忘记删除调试代码)。更新产品副标题字段后,您应该能够看到标题下的副标题。
    • 谢谢——我现在看到了自定义副标题字段。但是,副标题仍然出现在产品图片上方,而不是产品标题下方。非常感谢您的帮助。
    • 我找到了解决方案。非常感谢你的帮助。因为我使用的是 OceanWP 主题,所以我不得不将 'woocommerce_after_shop_loop_item_title' 替换为 'ocean_after_archive_product_title'。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    相关资源
    最近更新 更多