我有一个类别页面,如何在此页面上显示此类别同级类别?我知道如何显示儿童类别
我想我需要弄到term\\u id
在术语存档页上,例如example.com/category/foo/ (对于默认值category 分类学),
您可以使用get_queried_object() 检索所查询术语的术语对象(foo 在上面的示例URL中)。
$term = get_queried_object();
$term_id = $term->term_id;
您可以使用
get_queried_object_id() 仅检索所查询术语的术语ID。
$term_id = get_queried_object_id();
现在,对于检索术语的同级,您可以使用
get_terms() 就像你在检索学期的孩子时所做的那样。例如,对于被查询的术语(或者简单地说,当前术语),
$taxonomy_name = \'products\';
// Get the full term object/data.
$current_term = get_queried_object();
// Get the term\'s direct children.
// **Use \'child_of\' instead of \'parent\' to retrieve direct and non-direct children.
$children = get_terms( array(
    \'taxonomy\'   => $taxonomy_name,
    \'parent\'     => $current_term->term_id,
    \'hide_empty\' => false,
) );
// Display the children.
if ( ! empty( $children ) ) {
    echo "<h3>Children of $current_term->name</h3>";
    echo \'<ul>\';
    foreach ( $children as $child ) {
        echo "<li>$child->name</li>";
    }
    echo \'</ul>\';
}
// Get the term\'s direct siblings.
$siblings = get_terms( array(
    \'taxonomy\'   => $taxonomy_name,
    \'parent\'     => $current_term->parent,
    \'exclude\'    => array( $current_term->term_id ),
    \'hide_empty\' => false,
) );
// Display the siblings.
if ( ! empty( $siblings ) ) {
    echo "<h3>Siblings of $current_term->name</h3>";
    echo \'<ul>\';
    foreach ( $siblings as $sibling ) {
        echo "<li>$sibling->name</li>";
    }
    echo \'</ul>\';
}
如果你想使用
get_term_children(), 请注意,它检索所有直接子级和非直接子级(就像
get_terms() 如果
child_of ,函数返回一个术语数组
IDs,因此在您的
foreach, 您可以使用
get_term() 获取完整术语对象/数据。
$term_id = get_queried_object_id(); // or just use a specific term ID, if you want to
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child_id ) {
    $child = get_term( $child_id );
    // ... your code.
}
如何从此列表中排除我所在的现有类别
如果您使用get_terms(), 您可以使用exclude arg,就像您可以在上面的示例中看到的一样。
如果您使用get_term_children(), 然后您可以使用continue like so:(但这只是一个例子;你可以使用array_filter() 筛选$termchildren 如果需要,请在循环项目之前排列)
foreach ( $termchildren as $child_id ) {
    if ( (int) $child_id === (int) $term_id ) {
        continue;
    }
    $child = get_term( $child_id );
    // ... your code.
}