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.
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.