Skip to main content
1 of 2

Display the deepest child category from Category X (in loop)

I have a number of top-level categories including "News" and "Sport". They each have multiple levels of child categories as shown below.

  • News

    • Australia
      • NSW
        • Sydney
  • Sports

  • Cycling

    • Road Cycling
    • BMX
  • Triathlon

In my loop I want to display a single category - what ever is the deepest child category of "Sport".

I have found some code on StackOverflow that does returns the deepest child category, but not from a specific category. It returns the the deepest child category from all top-level categories. So if a post is in two categories (Sydney and Triathlon) the code shows Sydney as the deepest child category. I somehow need to specify that which top-level category I want the deepest child to come from. In this case that would be "Sport" and the deepest child cat would therefore be "Triathlon".