获取 Woocommerce 档案中当前产品类别的子类别

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

问题描述

我正在尝试在 Woocommerce 中显示当前类别下的子类别(不是子类别等),例如这个网站:http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en

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

例如,Construction 是类别,Sealants &粘合剂、防水材料、聚氨酯泡沫……属于子类别.

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

密封剂胶粘剂是类别,而醋酸硅酮密封胶、中性硅酮密封胶、丙烯酸密封胶…是子类别…

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>';
}

经过测试并有效.

用法示例:

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 档案中当前产品类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:将折扣百分比添加到销售的可变产品中 下一篇:将简短描述移动到 Woocommerce 单个产品页面的选项卡中

相关文章

最新文章