I have solved my problem. I added this definition before my old code:
<?php
// get the category object for the current category
$thisTrueCat = get_category( get_query_var( 'cat' ) );
print $thisTrueCat->term_id;
?>
The above $thisTrueCat
, different from $thisCat
in the already existing code, retrieves the ID of the current category regardless of it’s parent. $thisCat
on the other hand is set up to default back to the ID of the current category’s parent.
With $thisTrueCat
established, I then added the below code inside the loop:
<li <?php if ($thisTrueCat->term_id == $category->term_id) { ?> class="xshown" <?php } ?>
This will give the list item the class xshown if the current category displayed match the category of the list item. In other words, with CSS, I highlight the category currently displayed.
Here’s my new code in it’s entirety:
<!-- call ID of thisTrueCat -->
<?php
// get the category object for the current category
$thisTrueCat = get_category( get_query_var( 'cat' ) );
print $thisTrueCat->term_id; ?>
<ul class="submenu">
<!-- call submenu -->
<?php
// get the category object for the current category
$thisCat = get_category( get_query_var( 'cat' ) );
// if not top-level, track it up the chain to find its ancestor
while ( intval($thisCat->parent) > 0 ) {
$thisCat = get_category ( $thisCat->parent );
}
//by now $thisCat will be the top-level category, proceed as before
$args=array(
'child_of' => $thisCat->term_id,
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories( $args );
foreach( $categories as $category ) { ?>
<li <?php if ($thisTrueCat->term_id == $category->term_id) { ?> class="xshown" <?php } ?>><a href="https://wordpress.stackexchange.com/questions/143122/<?php echo get_category_link( $category->term_id ) ?>" ><?php echo $category->name ?></a></li>
<?php
};
?>
</ul>