在 Woocommerce 中显示当前子类别的 sub-subcategories
Display the sub-subcategories of the current subcategories in Woocommerce
我正在尝试在 Woocommerce 的当前子类别下显示 sub-subcategories,例如 this Website。
我有 2 parent 类 "Products" 和 "Sectors"。然后我有一个菜单 link 可以同时访问两者。
当我在 "Products" 时,我想查看子类别的图片、类别的标题,然后是所有带有标题的 sub-subcategories 和 link。
例如,parent 类别是 "Products",建筑是子类别,密封剂和粘合剂、防水材料、聚氨酯泡沫……是 sub-subcategories。
密封胶和胶粘剂是子类别,醋酸硅酮密封胶、中性硅酮密封胶、丙烯酸密封胶……是 sub-subcategories……
这是一张更好地解释它的截图:
此处使用的代码与您之前的问题线程非常相似。但是我们使用 特定的操作挂钩 并进行一些更改以获取子类别的子类别:
// 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 文件。已测试并有效。
Notes: The woocommerce_after_subcategory
action hook is located on content-product_cat.php
template file, which handle subcategories to be displayed as products are (with an image and a linked term name).
For that your main categories needs to have the option "Display type" set on "Subcategory".
我正在尝试在 Woocommerce 的当前子类别下显示 sub-subcategories,例如 this Website。
我有 2 parent 类 "Products" 和 "Sectors"。然后我有一个菜单 link 可以同时访问两者。
当我在 "Products" 时,我想查看子类别的图片、类别的标题,然后是所有带有标题的 sub-subcategories 和 link。
例如,parent 类别是 "Products",建筑是子类别,密封剂和粘合剂、防水材料、聚氨酯泡沫……是 sub-subcategories。
密封胶和胶粘剂是子类别,醋酸硅酮密封胶、中性硅酮密封胶、丙烯酸密封胶……是 sub-subcategories……
这是一张更好地解释它的截图:
此处使用的代码与您之前的问题线程非常相似。但是我们使用 特定的操作挂钩 并进行一些更改以获取子类别的子类别:
// 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 文件。已测试并有效。
Notes: The
woocommerce_after_subcategory
action hook is located oncontent-product_cat.php
template file, which handle subcategories to be displayed as products are (with an image and a linked term name).
For that your main categories needs to have the option "Display type" set on "Subcategory".