Archive for December, 2009

Capture all subcats in a category dynamically

Dec
2
2009

I’ve been working on two foreign language sites where they’re using a category of “English” (slug of “en”) to create an alternate page for English posts. In order to make certain elements on the category pages and single pages in the subcategories of “English” different than those on the pages in the native language, I had to write some code to dynamically pull in all of the subcategories of  “English”.

<?php $en = “229″;  // used the ID, not slug or name just in case they want to eventually rename the category
$i = 1;  // place holder variable
$sub_cats = get_categories(‘child_of=’.$en.’&style=none&hide_empty=0′); // get all children and store in array
foreach($sub_cats as $sub) { // go through all of the sub categories
$english_cats[$i] = $sub->cat_ID; // go through the array and just pull out the category IDs and store in new array
$i++; // go to the nextarray element

Category: Blog
Tags: ,