問題描述
我正在嘗試在 Woocommerce 中顯示當前類別下的子類別(不是子類別等),例如這個網(wǎng)站:http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en
I'm trying to Show subcategories (not subsubcategories,etc) under current category in Woocommerce like this Web: http://www.qs-adhesivos.es/app/productos/productos.asp?idioma=en
例如,Construction 是類別,Sealants &粘合劑、防水材料、聚氨酯泡沫……屬于子類別.
For Example, Construction is the category, and Sealants & adhesives, waterproofing, plyurethane foams… are subcategories.
密封劑膠粘劑是類別,而醋酸硅酮密封膠、中性硅酮密封膠、丙烯酸密封膠…是子類別…
Sealants & Mastics is the category, and ACETIC SILICONE SEALANT, NEUTRAL SILICONE SEALANT, ACRYLIC SEALANT… are subcategories…
在我的子主題下的 woocommerce 文件夾中已經(jīng)有一個 archive-product.php.
Already have an archive-product.php in a woocommerce folder under my child theme.
已經(jīng)嘗試了一些代碼并且它適用,但這不是我想要的.
Already tried some code and it applies but it's not what I want.
推薦答案
以下代碼將顯示產(chǎn)品類別存檔頁面的當前產(chǎn)品類別的格式化鏈接產(chǎn)品子類別:
The following code will display the formatted linked product subcategories from current product category for product category archive pages:
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>';
}
經(jīng)過測試并有效.
用法示例:
1) 您可以直接在 archive-product.php
模板文件中使用此代碼.
1) You can use this code directly in archive-product.php
template file.
2) 您可以將代碼嵌入到函數(shù)中,替換最后一行 echo $output .'</ul>';
由 return $output .'</ul>';
,對于短代碼,總是返回顯示.
2) You can embed the code in a function, replacing the last line echo $output . '</ul>';
by return $output . '</ul>';
, as for shortcodes, the display is always returned.
3) 您可以使用諸如 woocommerce_archive_description
之類的操作掛鉤來嵌入代碼:
3) You can embed the code using action hooks like 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 文件中.經(jīng)測試有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
要在類別描述后顯示,請將鉤子優(yōu)先級從 5
更改為 20
在:
To display it after the category description, change the hook priority from 5
to 20
in:
add_action('woocommerce_archive_description', 'display_subcategories_list', 5 );
喜歡:
add_action('woocommerce_archive_description', 'display_subcategories_list', 20 );
這篇關(guān)于獲取 Woocommerce 檔案中當前產(chǎn)品類別的子類別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!