简体   繁体   English

在 WooCommerce 单一产品页面上为特定产品添加自定义内容

[英]Add custom content for a specific product on WooCommerce single product pages

In Woocommerce, How can I add a custom content for a specific product on single product pages?在 Woocommerce 中,如何在单个产品页面上为特定产品添加自定义内容?

Here is an explicit screen shot:这是一个明确的屏幕截图: 如何为单个产品页面添加内容

As your screenshot is not so clear on where you want this custom content you have 2 options:由于您的屏幕截图不清楚您想要此自定义内容的位置,因此您有 2 个选项:

1) Under the product price 1)根据产品价格

With this custom function hooked in woocommerce_before_single_product_summary action hook you can add some custom content to a specific product ID (to be defined in the function) this way:使用woocommerce_before_single_product_summary操作挂钩中的此自定义函数,您可以通过以下方式将一些自定义内容添加到特定产品 ID (将在函数中定义)

add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
    global $product;

    // Limit to a specific product ID only (Set your product ID below )
    if( $product->get_id() != 37 ) return;

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    // End of content
}

2) Under the product image: 2) 在产品图片下:

With this custom function hooked in woocommerce_before_single_product_summary action hook you can add some custom content to a specific product ID (to be defined in the function) this way:使用woocommerce_before_single_product_summary操作挂钩中的此自定义函数,您可以通过以下方式将一些自定义内容添加到特定产品 ID (将在函数中定义)

add_action( 'woocommerce_before_single_product_summary', 'add_custom_content_for_specific_product', 25 );
function add_custom_content_for_specific_product() {
    global $product;

    // Limit to a specific product ID only (Set your product ID below )
    if( $product->get_id() != 37 ) return;

    // The content start below (with translatables texts)
    ?>
        <div class="custom-content product-id-<?php echo $product->get_id(); ?>">
            <h3><?php _e("My custom content title", "woocommerce"); ?></h3>
            <p><?php _e("This is my custom content text, this is my custom content text, this is my custom content text…", "woocommerce"); ?></p>
        </div>
    <?php
    // End of content
}

If you want to remove the product short description you can add into the function just after the if statement:如果要删除产品简短描述,可以在 if 语句之后添加到函数中:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 functions.php 文件中,也位于任何插件文件中。

Tested and works…经过测试和工作......

Just Edit the product to add the content.只需编辑产品即可添加内容。 and that content will show on its detail page.并且该内容将显示在其详细信息页面上。

The code upstairs working perfect.楼上的代码完美运行。 I want to ask, how is possible to moderate the function and to give permission to appearing a custom co我想问一下,如何调节功能并允许出现自定义 co

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 WooCommerce 特定 ID 产品页面上的多个产品中添加自定义内容 - Add custom content into multiple products on WooCommerce specific IDs product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 我想在特定的帖子页面中添加自定义字段,并在woocommerce单个产品页面中输出内容 - I want to add a custom field in a specific post page and output the content in woocommerce single product page Woocommerce 上单个产品页面中的自定义数量 - Custom quantities in single product pages on Woocommerce 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages WooCommerce单品页面添加OpenGraph标签 - Add an OpenGraph tag in WooCommerce single product pages 获取特定产品属性并将其显示在Woocommerce单一产品页面中 - Get specific product attribute and display it in Woocommerce Single Product Pages 在Woocommerce单一产品页面上排除特定的产品类别 - Exclude specific product categories on Woocommerce single product pages 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM