You can make use of the get_the_categories
filter to remove the parent categories from the list. the_category()
uses get_the_category_list()
which in turn uses get_the_category()
The idea is to check the categories returned against an array of parent ids and then removing those categories from the list.
You can try the following (Requires PHP 5.3+)
add_filter( 'get_the_categories', function ( $categories )
{
/**
* Count the amount of categories attached to the post
* If there is only one category, return that category
*/
if ( count( $categories ) == 1 )
return $categories;
/**
* Get an array of parent terms to test against the terms
*
* @see wp_list_pluck()
* @link https://codex.wordpress.org/Function_Reference/wp_list_pluck
*/
$parents = wp_list_pluck( $categories, 'parent' );
foreach ( $categories as $key => $category ) {
/**
* Test the category ID against $parents array
* If the category ID is the $parents array, unset it from the
* $category array
*/
if ( in_array( $category->term_id, $parents ) )
unset( $categories[$key] );
}
// Return new $categories array
return $categories;
});
EDIT
the_category( ', ' );
should return a list of categories separated by a comma. If only one category exists, it should display that category without any bullet points