1
votes

I'm working on a WordPress custom theme and stuck in achieving the following functionality. I don't even know is that even possible!

I want to display child categories when I click on a a parent category and then clicking child category will bring you posts from it. Doing it in archive.php will be suitable or a custom template?

Help and suggestions would greatly appreciated :)

1

1 Answers

1
votes

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 🙂