我使用的是需要在不同类别页面上显示特定内容的代码.

我的分类有以下 struct :

男人

  • 布料
      • 短裤
      • 短裤 with pockets

But the code below, displays content only on the parent category (男人), and the 1st level subcategory (布料):

add_action( 'woocommerce_archive_description', 'add_slide_text',1 );
function add_slide_text() {
    $cat = get_queried_object();

    if ( is_product_category() ) {
        if ( is_product_category( 'man' ) ||  $cat->parent === 233 ) {
            echo 'Hello man';
        } elseif ( is_product_category( 'woman' ) ||  $cat->parent === 232 ) {
            echo 'Hello woman';
        } else {
            echo '';
        }
    }
}

我如何才能强制它显示更低级别的子类别上的内容?例如,在"短裤"和"带口袋的短裤"中有(and possibly lower)个?

如有任何帮助,我们不胜感激.

推荐答案

您可以将get_term_children() WordPress function与WooCommerce产品类别自定义分类一起使用,以显示每个特定产品类别"男人"和"女人"术语及其子项的特定文本,如下所示:

add_action( 'woocommerce_archive_description', 'add_slide_text', 1 );
function add_slide_text() {
    // Targeting WooCommerce product category archives
    if ( is_product_category() ) {
        $current_term = get_queried_object();
        $taxonomy     = $current_term->taxonomy;

        // For "man" term (and term ID 233)
        $term_man_id      = get_term_by('slug', 'man', $taxonomy)->term_id; // Get "man" term ID
        $children_man_ids = (array) get_term_children($term_man_id, $taxonomy); // Get children terms IDs
        $man_terms_ids    = array_merge( array(233, $term_man_id), $children_man_ids ); // Merge terms IDs in a unique array

        // For "woman" term (and term ID 232)
        $term_woman_id      = get_term_by('slug', 'woman', $taxonomy)->term_id; // Get "woman" term ID
        $children_woman_ids = (array) get_term_children($term_woman_id, $taxonomy); // Get children terms IDs
        $woman_terms_ids    = array_merge( array(232, $term_woman_id), $children_woman_ids ); // Merge terms IDs in a unique array

        // Conditional text display
        if ( in_array( $current_term->term_id, $man_terms_ids ) ) {
            _e('Hello man', 'woocommerce');
        } 
        elseif ( in_array( $current_term->term_id, $woman_terms_ids ) ) {
            _e('Hello woman', 'woocommerce');
        }
    }
}

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

以编程方式更新现有Yith WooCommerce Booking的状态

LaravelEloquent 地根据表中的值提取记录

Laravel POST方法返回状态:POST方法上不允许使用405方法

在冲突和几何上插入的Postgres参数化查询在PHP中不起作用

将下拉 Select 的值从WooCommerce后端保存并显示到前端

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

上传WordPress模板中的翻译文件(.mo和.po)

如何在WordPress REST API中判断应用程序密码

在WooCommerce上设置简单和可变产品的最小订单量

将数据推入所有子数组条目

在 DateTime 或 DateTimeImmutable 对象上调用modify() 时是否必须使用空格?

Laravel 添加到经过身份验证的用户的数据将应用于每个查询

使用 Laravel 和 Vue 进行图像表单验证

注册命名空间后如何获取xml node 值?

设置GA4 PHP API的setSubject以允许访问所有管理员账户属性

如何使用动态变量定义 bind_result?

在 WordPress 中使用 $wpdb 运行 MySQL 事务是否安全?

正则表达式抓取递归括号的内容

如何在 Laravel 中验证多个输入?