Comments inline:
<?php
global $wp_query;
//* first let\'s get current post parent category ID
$ID = $wp_query->posts[0]->ID; //* get current post ID
$postcat = get_the_category($ID); //* based on post ID, get current post categories IDs -> WP function returns array of stdClass Objects, one stdClass Object for each category; if there is child - parent relationship, first it will return Objects for children categories and than for parents -> add "print_r ($postcat);" without quotation marks in next row to see content of array
$cat = $postcat[0]->cat_ID; //* get first category ID from first Object in $postcat array (if post has children categories, it will get first child ID, else it will get first category ID)
$thiscat = get_category ($cat); //* get array of category objects for $cat (current) category -> add "print_r ($thiscat);" without quotation marks in next row to see content of array
$parent = $thiscat->category_parent; //* and extract parent category ID; if category has no parent category (and parent category has no parent), ID = 0
//* now we have current category parent ID No, or if category has no children ID = 0
//* let\'s check if we heave categroy with children categories or not
if ($parent == 0) //* if parent ID = 0, it is category without parent category
{} //* do nothing
else { //* else, if it has children categories
$subcategories = get_categories(\'child_of=\'.$parent); //* get array of children categories Objects and
$items=\'\'; //* create new ArrayObject and
foreach($subcategories as $subcat) { //* foreach child category in array
if($thiscat->term_id == $subcat->term_id) //* check if current category ID is same as child category ID
$current = \' current-cat\'; //* if true, add current-cat to li class
else $current = \'\'; //* if not, add nothing
$items .= \'
<li class="cat-item cat-item-\'.$subcat->term_id.$current.\'"> <!-- if is child current, add here .current-cat //-->
<a href="\'.get_category_link( $subcat->term_id ).\'" title="\'.$subcat->description.\'">\'.$subcat->name.\' (\'.$subcat->count.\' posts)</a> <!-- link to -> category link, title -> category description, show -> category name and No of posts //-->
</li>\';
}
echo "<ul>$items</ul>";
}
?>