1 回答

TA贡献1873条经验 获得超9个赞
以下代码将显示产品类别存档页面的当前产品类别的格式化链接产品子类别:
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:
// 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 文件中。测试和工作。
要将其显示在类别描述之后,请将挂钩优先级从更改5为20in:
add_action('woocommerce_archive_description', 'display_subcategories_list', 5 );
喜欢:
add_action('woocommerce_archive_description', 'display_subcategories_list', 20 );
- 1 回答
- 0 关注
- 176 浏览
添加回答
举报