1 回答

TA貢獻1873條經(jīng)驗 獲得超9個贊
以下代碼將顯示產(chǎn)品類別存檔頁面的當前產(chǎn)品類別的格式化鏈接產(chǎn)品子類別:
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 文件中。測試和工作。
要將其顯示在類別描述之后,請將掛鉤優(yōu)先級從更改5為20in:
add_action('woocommerce_archive_description', 'display_subcategories_list', 5 );
喜歡:
add_action('woocommerce_archive_description', 'display_subcategories_list', 20 );
- 1 回答
- 0 關注
- 202 瀏覽
添加回答
舉報