PHP WordPress How To Display All Of Your Post Tags

PHP WordPress How To Display All Of Your Post Tags

Here is how you can display all of your post tags very easily. Add the code below to your functions.php and just place the shortcode wherever you want the tags to be shown Рa post, a page, a widget, or anywhere else.

function display_all_post_tags($attributes)
{
    $tags = get_tags('post_tag'); //taxonomy=post_tag
    
    $string =<<<CSS
<style>
.tagbox { 
background-color:#eee;
border: 1px solid #ccc;
margin:0px 10px 10px 0px;
line-height: 200%;
padding:2px 0 2px 2px;
 
}
.taglink  { 
padding:2px;
}
 
.tagbox a, .tagbox a:visited, .tagbox a:active { 
text-decoration:none;
}
 
.tagcount { 
background-color:black;
color:white;
position: relative;
padding:2px;
}	
</style>
CSS;
    
    foreach ($tags as $tag) { 
        //$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";
        $string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";
    } 
    return $string;
    
}
add_shortcode('display_all_post_tags', 'display_all_post_tags');

The code includes some simple styling, which you can modify to suit your own WordPress theme. On top of that, there is a commented line that is like so

//$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";

Currently, the code doesn’t display the number of post the tag is linked with. If you wish to display the post count, simply uncomment the above line of code in the snippet given above, and comment out the existing

$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";

If you need any help, let me know in the comments below.

Enjoyed the content ? Share it with your friends !