How to add a posts tags cloud to end of posts

Age of Article Warning:
This article was originally published 60 months ago. The information, tips and techniques explained may outdated. Examples shown on this page may no longer work. Please consider this when viewing the below content.

An alternative way of showing related content in your posts is to include a TAG CLOUD at the end of your article. You can create this with an easy shortcode function that will identify the post ID, get the post’s tags and then display the tags as a tag cloud.

Simply add the following shortcode code to your functions.php file, then add the shortcode [dtposts-tagcloud] in your posts where you would like the tag cloud to display.

// add a posts tags cloud to end of posts
function dtposts_tagcloud($echo=false) {
	global $post;
	$tag_ids= wp_get_post_tags($post->ID, array('fields'=>'ids'));
	if($tag_ids) {
		return wp_tag_cloud(array(
			'unit' => 'pt',
			'smallest' => '10',
			'largest' => '22',
			'order' => 'RAND',
			'include' => $tag_ids,
			'echo'=> $echo,
		} }

This uses the standard wp_tag_cloud() function as explained in the WordPress Codex.

This shortcode is demonstrated below. I have added a heading, some css styling, and then called the function using the shortcode. The code is entered as text in the text editor in a WordPress post:

<div style="text-align: center; padding: 20px; background-color: rgba(60,60,60,0.75); margin: 30px;"><b style="color: white; font-size: 18px; font-weight: bold;">POST TAGS</b>

Here is how it looks:

Check out some more tips and code snippets using the above tags.

Filed under: WordPressTagged with: , , ,