WordPress.org

Ready to get started?Download WordPress

Forums

get_categories and select default option for non-active dropdown items (1 post)

  1. lorensson
    Member
    Posted 1 year ago #

    Hi there, I have this category jump menu which lists child cats and always 'selects' the current category archive as 'selected' or active. I want to add a first <select> option titled "-choose a city-" that's always at the top, but only selected if NOT on a listed child category, such as a page or the site home. How do I do this?

    <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&orderby=title");
    					$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');?>/category/uk/"+dropdown.options[dropdown.selectedIndex].value;
    							}
    					    }
    					    dropdown.onchange = onCatChange;
    					--></script>
    				</form>

Topic Closed

This topic has been closed to new replies.

About this Topic