【问题标题】:Display the sub-subcategories of the current subcategories in Woocommerce在 Woocommerce 中显示当前子类别的子类别
【发布时间】:2020-01-06 08:03:54
【问题描述】:

我正在尝试在 Woocommerce 中的当前子类别下显示子子类别,例如 this Website。

我有 2 个父母类别“产品”和“行业”。然后我有一个指向两者的菜单链接。

当我在“产品”中时,我想查看子类别的图片、类别的标题,然后是带有标题的所有子子类别并链接到它们。

例如,父类别是“产品”,建筑是子类别,而密封剂和粘合剂、防水、聚氨酯泡沫……是子类别。

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

这是一个更能说明问题的屏幕截图:

【问题讨论】:

    标签: php wordpress woocommerce hierarchical taxonomy-terms


    【解决方案1】:

    此处使用的代码与您之前的问题线程非常相似。但是我们使用了特定的动作钩子并进行了一些更改来获取子类别的子类别:

    // Displaying the sub-subcategories of the current subategories
    add_action('woocommerce_after_subcategory', 'display_subsubcategories_list', 20, 1 );
    function display_subsubcategories_list( $category ) {
        $taxonomy = 'product_cat';
    
        // Get sub-subcategories of the current subcategory
        $terms    = get_terms([
            'taxonomy'    => $taxonomy,
            'hide_empty'  => true,
            'parent'      => $category->term_id
        ]);
    
        if( count($terms) > 0 ) :
    
        echo '<ul class="subcategories-list" style="list-style: none; border: solid 1px #ddd; border-bottom: none;">';
    
        // Loop through product sub-subcategories WP_Term Objects
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $taxonomy );
    
            echo '<li class="'. $term->slug .'" style="border-bottom: solid 1px #ddd;"><a href="'. $term_link .'">'. $term->name .'</a></li>';
        }
    
        echo '</ul>';
    
        endif;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    注意:woocommerce_after_subcategory 操作挂钩位于content-product_cat.php 模板文件中,该模板文件处理要显示为产品的子类别(带有图像和链接的术语名称)。
    为此,您的主要类别需要在“子类别”上设置“显示类型”选项。

    【讨论】:

    • 还有一个问题,在哪里可以更改“子类别标题”的字体颜色和背景颜色(字体颜色为白色,背景为黑色,如截图)?
    • @Alvaro 在主题 style.css 文件中,或者如果您使用的主题有一个带有外观选项的界面,您将在那里进行更改。如果您不熟悉样式,请在线阅读一些基本的 CSS 教程。
    猜你喜欢
    • 2021-01-09
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    相关资源
    最近更新 更多