Add Link Capability to Select Dropdown

The below example uses WordPress Tags.

<h2>TAGS</h2>

<select name="menu1" id="menu1">
	<option>Select a Tag</option>
	<?php $tags_array = get_tags( $args ); 
	
	$tags = get_tags();
	
	foreach ( $tags as $tag ) {
		$tag_link = get_tag_link( $tag->term_id ); ?>

		<option value="<?php echo $tag_link; ?>"><?php echo $tag->name; ?></option>
	
	<?php } ?>
</select>

jQuery

<script type="text/javascript">
	document.getElementById("menu1").onchange = function() {
        if (this.selectedIndex!==0) {
            window.location.href = this.value;
        }
    };
</script>

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.