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

Get the subcategories of the current product category in Woocommerce archives(获取 Woocommerce 档案中当前产品类别的子类别)
本文介绍了获取 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 档案中当前产品类别的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)