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 post count of all wordpress tags

In this article, wordpress tutorial, how to display post count of all wordpress tags. In wordpress post we use post tags. For SEO purpose we can show post tags.

how to display post count of all wordpress tags

If we want to show the post count related to wordpress tags.

You can use following code:

<?php
//list all tags that are assigned to posts
$taxonomy = 'post_tag';
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
if ($term->count > 0) {
echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
}
}
}
?>
how to display post count of all wordpress tags
how to display post count of all wordpress tags