Display Popular Tags in WordPress

Display Popular Tags in WordPress
1849 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

3 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; 
      }

Leave a comment

Your email address will not be published.