获取子类别中具有相同 parent 的子类别列表

Get list of subcategories in subcategory with the same parent

也许标题有点奇怪,但我遇到了这个我想不通的问题。我得到的代码是获取 parent 类别中所有子类别的列表。

我的分类结构如下:

当您位于父类别时,您会看到所有子类别的列表。但是当你在子类别存档页面上时,列表就消失了。

这很有道理,但我如何确保当您在子类别页面上时,它会保持列表不变。显示与当前子类别具有相同 parent 类别的所有类别。

是否可以在我使用的这段代码中执行此操作 "archive-product.php"

<?php

    $term = get_queried_object();

    $children = get_terms( $term->taxonomy, array(
        'parent'    => $term->term_id,
        'hide_empty' => false
    ) );

    if ( $children ) { 
        foreach( $children as $subcat )
        {
            echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
        }
    }
?>

只要检查一下您的孩子是否空着使用:

$neighbors = get_terms( [
    'taxonomy'   => $term->taxonomy,
    'parent'     => $term->parent,
    'hide_empty' => false
] );