Found a solution myself and sharing it so it will help someone who is in need:
Well I have done it using archive.php
and I used get_queried_object()
to get the currently-queried object which gives me an object by doing the following:
$obj = get_queried_object();
print_r($obj);
and it will gives us the following object:
WP_Term Object
(
[term_id] => 24
[name] => BRIDAL
[slug] => bridal
[term_group] => 0
[term_taxonomy_id] => 24
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 0
[filter] => raw
[cat_ID] => 24
[category_count] => 0
[category_description] =>
[cat_name] => BRIDAL
[category_nicename] => bridal
[category_parent] => 0
)
You can see that there is a [parent] => 0
in the object above. So in my case I did it like this:
$obj = get_queried_object();
if ($obj->parent == 0) {
// Display child categories on this cat
} else {
// Display posts of the child category
}
Hope it will be helpful to someone 🙂