php - 获取 Woocommerce 文件中当前产品类别的子类别

标签 php wordpress woocommerce custom-taxonomy taxonomy-terms

<分区>

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

例如,建筑是类别,密封剂和粘合剂、防水 Material 、聚氨酯泡沫……是子类别。

密封剂和胶粘剂是类别,ACETIC SILICONE SEALANT、NEUTRAL SILICONE SEALANT、ACRYLIC SEALANT……是子类别……

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

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

最佳答案

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

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中使用这段代码模板文件。

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

3) 您可以使用类似 woocommerce_archive_description 的操作 Hook 嵌入代码:

// 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 文件。经过测试并有效。


要在类别描述之后显示它,请将 Hook 优先级从 5 更改为 20 在:

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

喜欢:

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

关于php - 获取 Woocommerce 文件中当前产品类别的子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57767843/

相关文章:

php - WooCommerce 购物车和结账中运输(标签)方法的额外 HTML

php - Woocommerce 结帐页面上的额外 paypal 费用

php - 数据表Js : Search not working for JOIN table result

javascript - 更改包含在 <td> 标记中的某些值的字体颜色

php - 将完整的 wordpress 站点转换为 HTTPS

php - 如何在 WordPress 上添加 GET 参数的链接,而参数和当前页面之间没有斜杠?

php - 客户处理订单电子邮件通知中的 Woocommerce 订单日期时间

php - Paypal 整合 : Please return to the payment page and correct the address

php - 使用 Ajax 和 MySql 分离结构

php - 在管理员的 WooCommerce 电子邮件通知中显示自定义字段