get top parent category
-
I’m developing a new theme which uses several main parent categories each with child categories and grand-child categories (and possibly great-grand-child categories).
The user will have the ability to add more sub-categories.
I want to style each post depending on its top parent.
Example structure:
New York
– NY maps
– NY social
– NY music
– NY art
Paris
– Paris maps
– Paris social
– Paris music
– Paris art
London
– London maps
– London social
– London music
– London artI’ve tried using the following as single.php to include the correct template file but this only seems to work for the next level parent not the absolute top level.
<?php $category = get_the_category(); $parent = get_cat_name($category[0]->category_parent); if ($parent == 'ny' ) { include(TEMPLATEPATH . '/single-ny.php'); } else if ($parent == 'paris' ) { include(TEMPLATEPATH . '/single-paris.php'); } else if ($parent == 'london' ) { include(TEMPLATEPATH . '/single-london.php'); } else { include(TEMPLATEPATH . '/single-norm.php'); } ?>I’ve been struggling with this for over a day now and I have exhausted all possibilities via google!!
Please help! There’s got to be a way of doing this!
The topic ‘get top parent category’ is closed to new replies.