在Woocommerce档案中获取当前产品类别的子类别 [英] Get the subcategories of the current product category in Woocommerce archives

查看:70
本文介绍了在Woocommerce档案中获取当前产品类别的子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Woocommerce的当前类别下显示子类别(而不是子类别等),例如:Web

I'm trying to Show subcategories (not subsubcategories,etc) under current category in Woocommerce like this Web: http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en

例如,建筑"是类别,而密封剂"和密封剂"粘合剂,防水,聚氨酯泡沫……是子类别.

For Example, Construction is the category, and Sealants & adhesives, waterproofing, plyurethane foams… are subcategories.

密封剂和Mastics是类别,而ACETIC SILICONE SEALANT,中性SILICONE SEALANT,ACRYLIC SEALANT…是子类别…

Sealants & Mastics is the category, and ACETIC SILICONE SEALANT, NEUTRAL SILICONE SEALANT, ACRYLIC SEALANT… are subcategories…

在我的子主题下的woocommerce文件夹中已经有一个archive-product.php.

Already have an archive-product.php in a woocommerce folder under my child theme.

已经尝试了一些代码,它适用,但这不是我想要的.

Already tried some code and it applies but it's not what I want.

推荐答案

以下代码将显示产品类别归档页面中当前产品类别的格式化链接产品子类别:

The following code will display the formatted linked product subcategories from current product category for product category archive pages:

if ( is_product_category() ) {

    $term_id  = get_queried_object_id();
    $taxonomy = 'product_cat';

    // Get subcategories of the current category
    $terms    = get_terms([
        'taxonomy'    => $taxonomy,
        'hide_empty'  => true,
        'parent'      => get_queried_object_id()
    ]);

    $output = '<ul class="subcategories-list">';

    // Loop through product subcategories WP_Term Objects
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, $taxonomy );

        $output .= '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';
    }

    echo $output . '</ul>';
}

经过测试,可以正常工作.

Tested and works.

用法示例:

1)您可以直接在 archive-product.php 模板文件中使用此代码.

1) You can use this code directly in archive-product.php template file.

2)您可以将代码嵌入函数中,替换最后一行 echo $ output.'</ul>'; 通过 return $ output.'</ul>'; ,对于简码,总是返回显示.

2) You can embed the code in a function, replacing the last line echo $output . '</ul>'; by return $output . '</ul>';, as for shortcodes, the display is always returned.

3)您可以使用动作钩子来嵌入代码,例如 woocommerce_archive_description :

3) You can embed the code using action hooks like woocommerce_archive_description:

// Displaying the subcategories after category title
add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 
function display_subcategories_list() {
    if ( is_product_category() ) {

        $term_id  = get_queried_object_id();
        $taxonomy = 'product_cat';

        // Get subcategories of the current category
        $terms    = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => true,
            'parent'      => $term_id
        ]);

        echo '<ul class="subcategories-list">';

        // Loop through product subcategories WP_Term Objects
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $taxonomy );

            echo '<li class="'. $term->slug .'"><a href="'. $term_link .'">'. $term->name .'</a></li>';
        }

        echo '</ul>';
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

要在类别描述后显示它,请在以下位置将挂接优先级从 5 更改为 20 :

To display it after the category description, change the hook priority from 5 to 20 in:

add_action('woocommerce_archive_description', 'display_subcategories_list', 5 ); 

喜欢:

add_action('woocommerce_archive_description', 'display_subcategories_list', 20 ); 

这篇关于在Woocommerce档案中获取当前产品类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆