add parent cat to drop down cat list?
-
Hi guys, I am using this code to generate a list of 2nd-level categories so it output a
<option>/<select>
list of category links, but I want to include the parent category’s name at the top of the <option> list without making it selectable – just as a header/default value. How would I grab the parent category’s name and put it in at the top of the dropdown list? (Cat ID 33).<form id="cities" method="post"> <?php function replace_id_for_slug($option){ $categories = get_categories("hierarchical=1&echo=0&child_of=33"); preg_match('/value="(\d*)"/', $option[0], $matches); $id = $matches[1]; $slug = ""; foreach($categories as $category){ if($category->cat_ID == $id){ $slug = $category->slug; } } return preg_replace("/value=\"(\d*)\"/", "value=\"$slug\"", $option[0]); } $select = wp_dropdown_categories("hierarchical=1&echo=0&child_of=33&depth=1"); $select = preg_replace_callback("#<option[^>]*>[^<]*</option>#", "replace_id_for_slug", $select); echo $select; ?> <script type="text/javascript"><!-- var dropdown = document.getElementById("cat"); function onCatChange() { if ( dropdown.options[dropdown.selectedIndex].value != -1 ) { location.href = "<?php echo get_option('home');?>/uk/"+dropdown.options[dropdown.selectedIndex].value; } } dropdown.onchange = onCatChange; --></script> </form>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘add parent cat to drop down cat list?’ is closed to new replies.