首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为产品的自定义内容在woocommerce上的php代码上添加更多i?

如何为产品的自定义内容在woocommerce上的php代码上添加更多i?
EN

Stack Overflow用户
提问于 2022-07-22 20:00:05
回答 1查看 29关注 0票数 0

我是PHP世界的新手,我想问,我如何在这段代码中添加更多的产品I?

不幸的是,我不知道如何分离多个ID。

我想输入多个is,以便特定的内容只显示在某些产品上。

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-22 21:37:44

如果要在特定产品上显示内容,可以使用数组()

代码语言:javascript
复制
add_action( 'woocommerce_single_product_summary', 'add_custom_content_for_specific_product', 15 );
function add_custom_content_for_specific_product() {
    global $product;

    /* Add the product ids to the array separated by commas that you want the content below to display on
     * in_array() arguments:
     * $product->get_id() is the needle - what you're looking for in the array
     * [1,2,3,4,5] are the product ids you want to show the content on. Replace those with your actual ids
     * TRUE just makes sure it's the same type of data. In this case we set it to TRUE, since $product->get_id() is an integer and so is the data in the array.
    */ 
    if( in_array( $product->get_id(), [37,1,2,3,6,7], TRUE ) ) :

    // 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
    /* If the ids are not in the array above, return the function without output. */
    else :
        return;
    endif;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73085532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档