0

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:

<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
        <?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 class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">';
                            echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($subcat->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $subcat->name .' Sub Category Image"> </div>';
                            echo '<div class="category-title">' . $subcat->name . '</div></a></li>';
                    }
            }
            ?>
         </ul>

Now i am trying to figure out how to display siblings... Should i call parent ID and children after? I think i need to get term_id somehow, i am sure i am missing something:(

             <ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
             <?php
             $term_id = 10;
             $taxonomy_name = 'products';
             $termchildren = get_term_children( $term_id, $taxonomy_name );

                 if ( $termchildren ) {
                         foreach ( $termchildren as $child ){
                                 echo '<li class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($child, $child->taxonomy)) . '">';
                                 echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($child->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $child->name .' Sub Category Image"> </div>';
                                 echo '<div class="category-title">' . $child->name . '</div></a></li>';
                         }
                 }
                 ?>
                </ul>

i assume, but how i exclude existing category i am on from this list. Hope it makes sense:) Please help anyone?

UPDATE: So i found the solution:

<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
<?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 class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">';
                echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($subcat->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $subcat->name .' Sub Category Image"> </div>';
                echo '<div class="category-title">' . $subcat->name . '</div></a></li>';
        }
}
?>
</ul>
<ul class="slider-container-list sub-categories-block front-categories-block front-categories-scroll">
 <?php
     $term = get_queried_object();
     $siblings = get_terms( $term->taxonomy, array(
         'parent'    => $term->parent,
         'taxonomy'   => $taxonomy_name,
         'exclude'    => array( $current_term->term_id ),
         'hide_empty' => false,
     ) );
     if ( $siblings ) {
             foreach( $siblings as $sibling ){
                     echo '<li class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($sibling, $sibling->taxonomy)) . '">';
                     echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($sibling->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $sibling->name .' Sub Category Image"> </div>';
                     echo '<div class="category-title">' . $sibling->name . '</div></a></li>';
             }
     }
     ?>
    </ul>

I am able to display siblings here, but now my challenge is to

  1. Exclude the existing category, that i am on.
  2. And not to display siblings for the top parent categories

Fun fun!

2 Answers 2

1

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.
    }
    
6
  • Please look at the answer above:) Updated my post. And i found solution for #2, adding class to the top category, and using css to remove it: add_filter( 'body_class', 'custom_cat_archiev_class' ); function custom_cat_archiev_class( $classes ) { if ( is_category() ) { $cat = get_queried_object(); $ancestors = get_ancestors( $cat->term_id, 'category', 'taxonomy' ); $classes[] = 'category-level-' . ( count( $ancestors ) + 1 ); } return $classes; } Jan 18, 2022 at 19:28
  • So i am still looking for solution for #1. Thanks! Jan 18, 2022 at 19:32
  • Well, did my answer answered your main question - how to retrieve and display the term's siblings? And when you said, "exclude existing category i am on from this list", I actually thought something like, if you're on category 10, you wanted 10 to be removed from the array (of terms)? If not, or if you're referring to another taxonomy, then that should be asked in a separate post. Or if you're actually looking for CSS solution/help, then Stack Overflow is better suited for that.
    – Sally CJ
    Jan 18, 2022 at 23:00
  • Also, if you look at the get_terms() documentation, it stated that the 2nd parameter is deprecated and it also says, "Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:", e.g. $terms = get_terms( array( 'taxonomy' => 'products', 'parent' => 10, 'hide_empty' => false ) );, just like in my examples. :-) So always use the get_terms( <args array> ) format and not get_terms( <taxonomy>, <args array> ), unless of course if you're still using WordPress prior to v4.5.0 ..
    – Sally CJ
    Jan 18, 2022 at 23:29
  • 1
    Thank you Sally i did not know that! Appreciate your help! Jan 19, 2022 at 20:54
0
<ul class="display-children-block slider-container-list sub-categories-block front-categories-block front-categories-scroll">
<?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 class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">';
        echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($subcat->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $subcat->name .' Sub Category Image"> </div>';
        echo '<div class="category-title">' . $subcat->name . '</div></a></li>';
}
}
?>
</ul>
<ul class="display-siblings-block slider-container-list sub-categories-block front-categories-block front-categories-scroll">
    <?php
$term = get_queried_object();
$siblings = get_terms( $term->taxonomy, array(
 'parent'    => $term->parent,
   'taxonomy'   => $taxonomy_name,
   'exclude'    => array( $term->term_id ),
   'hide_empty' => false,
) );

if ( $siblings ) {
     foreach( $siblings as $sibling ){
             echo '<li class="category-thumbnail-item"><a class="category-thumbnail" href="' . esc_url(get_term_link($sibling, $sibling->taxonomy)) . '">';
             echo '<div class="category-thumbnail-image"><img src="' . z_taxonomy_image_url($sibling->term_id, 'medium'). '" data-pin-nopin="true" alt="'. $sibling->name .' Sub Category Image"> </div>';
             echo '<div class="category-title">' . $sibling->name . '</div></a></li>';
     }
}
?>
</ul>

This is what i did for all of it and it works!

Also this snippet add class to the :

add_filter( 'body_class', 'custom_cat_archiev_class' );
function custom_cat_archiev_class( $classes ) {
    if ( is_category() ) {
        $cat = get_queried_object();
        $ancestors = get_ancestors( $cat->term_id, 'category', 'taxonomy' );
        $classes[] = 'category-level-' . ( count( $ancestors ) + 1 );
    }
    return $classes;
}

And i am using css to remove siblings from top level Categories.

Honestly i think i can make it cleaner without classes and css. But i am not sure how!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.