How to display child categories of current category’s parent category?

I’m trying to display a list of categories in a PHP enabled text widget. On child category pages I need to get the id of the current category’s parent category and then use that to return a list of all that category’s child categories except the current category.

I tried the following code but it’s not working:

<?php if (is_category( )) {

$thiscat = get_category( get_query_var( 'cat' ) );
$catid = $thiscat->cat_ID;
$parent = $catid->category_parent;

$catlist = get_categories(
        array(
        'child_of' => $parent,
        'orderby' => 'id',
        'order' => 'DESC',
        'exclude' => $catid,
        'hide_empty' => '0'
        ) );
} ?>

This displays both the parent categories and child categories.

2 Answers
2

I now realize there’s an easier way to do this:

<?php   if (is_category( )) {

        $thiscat = get_category( get_query_var( 'cat' ) );
        $catid = $thiscat->cat_ID;
        $parent = $thiscat->category_parent;

        if (!empty ($parent) ) {
        //child category pages

        $catlist = get_categories(
        array(
        'child_of' => $parent,
        'orderby' => 'id',
        'order' => 'DESC',
        'exclude' => $catid,
        'hide_empty' => '0'
        ) );
}
} ?>

Leave a Comment