In wordpress get all tags in drop down terms

WordPress tags are very important. For SEO we use tags. Here in this article we will show post tags in drop down box using simple php script.

Little Background about creating tag drop down

For one of my wordpress plugin, I want to show tags drop down in widget setting form. I created plugin called “Smart Posts Widget“. For this wordpress plugin, I created tag drop down.

In wordpress get all tags in drop down terms
In wordpress get all tags in drop down terms

I used following code for creating drop down. If you want to create wordpress post tag drop down for some setting or configuration use following simple code.


<?php
$taxonomies=get_taxonomies('','object');
foreach($taxonomies as $tax){
echo "<h2>$tax->name</h2>";
$terms = get_terms( $tax->name, 'orderby=count&hide_empty=0' );
foreach($terms as $term){
print_a($term->name);
}
}
?>

Above is raw code, which I modified little bit to create drop down list of wordpress post tags.

get all tags in drop down terms

For tag drop down you can use following code.


<p style="display:<?php if($instance["cat_tag"]=='tag'){echo 'block';}else {echo 'none';} ?>" class="<?php echo $this->get_field_id('selected_tag'); ?>">
<label for="<?php echo $this->get_field_id('selected_tag'); ?>">
<?php _e( 'Tags' ); ?>:
<select id="<?php echo $this->get_field_id('selected_tag'); ?>" name="<?php echo $this->get_field_name('selected_tag'); ?>" >
<?php $tags = get_tags('orderby=name&order=ASC'); foreach($tags as $tag) {?>
<option value="<?php echo $tag->term_id;?>"<?php selected($instance['selected_tag'],$tag->term_id); ?> ><?php echo $tag->name; ?></option>
<?php }?>
</select>
</label>
</p>

Above Code in used in smart posts widget plugin, which is working fine with latest wordpress versions.

 

how to display wordpress categories in a drop down menu

wordpress tutorial, how to display wordpress categories in a drop down menu. For showing the categories in drop down menu just use our following code.

how to display wordpress categories in a drop down menu

You just need to copy paste following code in put in functions.php file.


<li id="categories"><h2><?php _e('Posts by Category'); ?></h2>
 <?php wp_dropdown_categories('show_option_none=Select category'); ?>

<script type="text/javascript"><!--
 var dropdown = document.getElementById("cat");
 function onCatChange() {
 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
 location.href = "<?php echo get_option('home');
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
 }
 }
 dropdown.onchange = onCatChange;
--></script>
</li>

For more information you can use the following code.

how to display wordpress categories in a drop down menu
how to display wordpress categories in a drop down menu