显示动态 WooCommerce 兄弟姐妹和直接 children 类别列表
Display a dynamic WooCommerce siblings and direct children category list
正在尝试创建显示当前产品类别的兄弟和第一级 children 的函数。所以不是兄弟姐妹的 children 或 children 的 children。
类别可以深入 4 个级别,我需要列表根据当前页面更改显示的内容。
示例:
- 类别 1
- 类别 2
- 类别 3 <-- 当前产品类别
- 类别 3-1
- 类别 3-2
- 类别 3-3
- 类别 3-4
- 类别 4
- 类别 5
我找到了一些解释显示兄弟姐妹的帖子,或者第一级children。但我试图将两者结合起来,但没有成功:
有什么想法吗?任何能给我指明正确方向的东西都会有所帮助。
经过反复试验,我终于弄明白了:
function wc_cat_menu() {
$queried_object = get_queried_object();
$taxonomy = 'product_cat';
$sibling_terms = get_terms( array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $queried_object->parent
) );
echo '<ul>';
foreach( $sibling_terms as $sibling ) {
if ( $sibling->parent > 0 ) {
$sibling_id = $sibling->term_id;
$siblingChildren = get_terms( $taxonomy, array(
'parent' => $sibling_id,
'hide_empty' => false
) );
echo '<li><a href="' . get_term_link( $sibling ) . '">' . $sibling->name . '<span> (' . $sibling->count . ')</span></a>';
if( $siblingChildren ) {
echo '<ul>';
foreach ( $siblingChildren as $child ) {
echo '<li><a href="' . get_term_link( $child, $taxonomy ) . '">' . $child->name . '<span> (' . $child->count . ')</span></a></li>';
}
echo '</ul>';
}
echo '</li>';
}
}
echo '</ul>';
}
过程中收获良多!可能有一个更有效的方法来实现这一点,欢迎任何建议。
正在尝试创建显示当前产品类别的兄弟和第一级 children 的函数。所以不是兄弟姐妹的 children 或 children 的 children。
类别可以深入 4 个级别,我需要列表根据当前页面更改显示的内容。
示例:
- 类别 1
- 类别 2
- 类别 3 <-- 当前产品类别
- 类别 3-1
- 类别 3-2
- 类别 3-3
- 类别 3-4
- 类别 4
- 类别 5
我找到了一些解释显示兄弟姐妹的帖子,或者第一级children。但我试图将两者结合起来,但没有成功:
有什么想法吗?任何能给我指明正确方向的东西都会有所帮助。
经过反复试验,我终于弄明白了:
function wc_cat_menu() {
$queried_object = get_queried_object();
$taxonomy = 'product_cat';
$sibling_terms = get_terms( array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $queried_object->parent
) );
echo '<ul>';
foreach( $sibling_terms as $sibling ) {
if ( $sibling->parent > 0 ) {
$sibling_id = $sibling->term_id;
$siblingChildren = get_terms( $taxonomy, array(
'parent' => $sibling_id,
'hide_empty' => false
) );
echo '<li><a href="' . get_term_link( $sibling ) . '">' . $sibling->name . '<span> (' . $sibling->count . ')</span></a>';
if( $siblingChildren ) {
echo '<ul>';
foreach ( $siblingChildren as $child ) {
echo '<li><a href="' . get_term_link( $child, $taxonomy ) . '">' . $child->name . '<span> (' . $child->count . ')</span></a></li>';
}
echo '</ul>';
}
echo '</li>';
}
}
echo '</ul>';
}
过程中收获良多!可能有一个更有效的方法来实现这一点,欢迎任何建议。