Display sibling categories on category page

I have a category page, how do i display this category sibling
categories on this page as well? I know how to display children
categories

I think i need to get term_id somehow

On a term archive page, e.g. at example.com/category/foo/ (for the default category taxonomy),

  1. You can use get_queried_object() to retrieve the term object for the term being queried (foo in the above sample URL).

    $term = get_queried_object();
    $term_id = $term->term_id;
    
  2. You can use get_queried_object_id() to retrieve just the term ID of the term being queried.

    $term_id = get_queried_object_id();
    

Now as for retrieving term’s siblings, you can use get_terms() just like you did when retrieving the term’s children. So for example, for the term being queried (or simply put, the current term),

$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>';
}

And if you want to use get_term_children(), note that it retrieves all direct and non-direct children (just like get_terms() if child_of is used), and the function returns an array of term IDs, hence in your foreach, you can use get_term() to get the full term object/data.

$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.
}

how i exclude existing category i am on from this list

  • If you use get_terms(), you can use the exclude arg like you can see in my example above.

  • If you use get_term_children(), then you can use continue like so: (but that’s just an example; you can use array_filter() to filter the $termchildren array before looping through the items, if you want to)

    foreach ( $termchildren as $child_id ) {
        if ( (int) $child_id === (int) $term_id ) {
            continue;
        }
    
        $child = get_term( $child_id );
        // ... your code.
    }