Display Popular Tags in WordPress

Display Popular Tags in WordPress
7598 Views

you’ll need to add following code to your current themes functions.php file.

<?php
function cxc_tag_cloud() { 
	$tags = get_tags();
   if( $tags ){
        foreach ( $tags as $tag ) {
            $tag_link = get_tag_link( $tag->term_id );
            $tag->link = $tag_link;
        }    
   }
	$args = array(
		'smallest'                  => 10, 
		'largest'                   => 22,
		'unit'                      => 'px', 
		'number'                    => 10,  
		'format'                    => 'flat',
		'separator'                 => " ",
		'orderby'                   => 'count', 
		'order'                     => 'DESC',
		'show_count'                => 1,
		'echo'                      => false
	); 

	$tag_string = wp_generate_tag_cloud( $tags, $args );

	return $tag_string; 

} 
// Add a shortcode so that we can use it in widgets, posts, and pages
add_shortcode( 'cxc_popular_tags', 'cxc_tag_cloud' ); 

// Enable shortcode execution in text widget
add_filter( 'widget_text', 'do_shortcode' );
?>

This code simply generates the top 10 tags from your website in a cloud with number of posts in each tag. After that it creates a shortcode cxc_popular_tags and enables shortcode in text widget.

You can now add shortcode [cxc_popular_tags] in any post, page, or widget to display your most popular tags.

In WordPress, how can I make tags visible?

Add the ‘Tag Cloud’ widget to the sidebar by going to Appearance » Widgets. The widget will expand, allowing you to view its options.

How can I include popular posts in WordPress?

Open the editor on any page or post. Then, in the upper left corner, click the + symbol and pick the Popular Posts or Shortcode blocks.

How can I display popular posts?

Please check this tutorial. Click here

Was this article helpful?
YesNo

12 comments

  1. Hello. Your code shows tags/tag cloud as you say, but they are not linked to tag pages. You can click them but you are always getting on homepage. Any idea how to fix it?

    1. Please use this function :
      /* Show tags with own link pages */

      
      function cxc_tag_cloud() { 
          $tags = get_tags();    
          if( $tags ){
              foreach ( $tags as $tag ) {
                  $tag_link = get_tag_link( $tag->term_id );
                  $tag->link = $tag_link;
              }    
          }
      
          $args = array( 'smallest' => 10, 
              'largest'                   => 22,
              'unit'                      => 'px', 
              'number'                    => 10,  
              'format'                    => 'flat',
              'separator'                 => " ",
              'orderby'                   => 'count', 
              'order'                     => 'DESC',
              'show_count'                => 1,
              'echo'                      => false
          ); 
          $tag_string = wp_generate_tag_cloud( $tags, $args );
          return $tag_string; 
      }
  2. I’m not that much of an online reader, to be honest, but your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back down the road. All the best

  3. Wow! After all, I got a webpage from where I can really get valuable facts regarding my study and knowledge.

  4. Hi to everyone, I am really eager to read this webpage’s post to be updated daily. It consists of pleasant data.

  5. I’m bored at work so I decided to browse your site on my iPhone during lunch break. I love the knowledge you provide here and can’t wait to take a look when I get home. I’m shocked at how quickly your blog loaded on my mobile. I’m not even using WIFI, just 3G.

    Anyhow, awesome site!

  6. Very great post. I just stumbled upon your blog and wanted to mention that I have really loved browsing your blog posts.

    After all, I will be subscribing to your feed and I’m hoping you write once more soon!

  7. Awesome things here. I am very happy to see your article.
    Thanks so much and I’m having a look ahead to contact you.

  8. It’s not my first time to visit this web page, I am visiting this web page daily and get good facts from here all the time.

  9. Thank you, I’ve recently been looking for information approximately this topic for a while and yours is the greatest I have found out till now.

Leave a comment

Your email address will not be published. Required fields are marked *