WordPress Tag Cloud

This page is for notes on various Category/Tag /Taxonomy Cloud functions. I’ve tested and fiddled with all sorts of plug-ins because every client tends to have some other unknown plug-in installed or some custom function.

WordPress has a built-in Tag Cloud function but its very basic.

Cat/Tag /Taxonomy Cloud Plugins.

Category to Pages WUD

Short code Cheat Sheet

[[wudcatlist]] = Displays categories as list.

Extra parameters:

  • max=”x”,  where X is the maximum records to show
  • flat=”1″,  shows the categories next to each other instead as list
  • title=”x”, shows a custom title instead the title in the admin page
[[wudtaglist]] = Displays tags as list.

Extra parameters:

  • max=”x”,  where X is the maximum records to show
  • flat=”1″,  shows the categories next to each other instead as list
  • title=”x”, shows a custom title instead the title in the admin page
[[wudpagelist]] = Displays page links from the selected category, inside a page.

Extra parameters:

  • max=”x”,  where X is the maximum records to show
  • flat=”1″,  shows the categories next to each other instead as list
  • title=”x”, shows a custom title instead the title in the admin page

[[wudrelated]] = Displays all related post and/or pages.

Extra parameters: 

  • max=”x”,  where X is the maximum records to show
  • flat=”1″,  shows the categories next to each other instead as list
  • title=”x”, shows a custom title instead the title in the admin page
  • include=”category”, shows also the related categories
  • type=”X”, where X = “page” or “post” and displays all related pages or posts. This parameter cannot be used together with the others.
[[wudcatdrop]] = Displays categories as drop down.
[[wudtagdrop]] = Displays tags as drop down.

All the above shortcodes can have the extra sort order parameter:

  • sort=”X”, where “X”  =
    • “date+”, “date-“, to sort by publish date
    • “name+”, “name-“, to sort by title
    • random

Tag Cloud Coding

Built-in function

<?php $args = array(
'smallest' => 8,
'largest' => 22,
'unit' => 'pt',
'number' => 45,
'format' => 'flat',
'separator' => "\n",
'orderby' => 'name',
'order' => 'ASC',
'exclude' => null,
'include' => null,
'topic_count_text_callback' => default_topic_count_text,
'link' => 'view',
'taxonomy' => 'post_tag',
'echo' => true,
'child_of' => null,
// see Note!); ?>

<?php wp_tag_cloud( $args ); ?>
Use the built in function to show categories
<?php wp_tag_cloud( array( 'taxonomy' => 'category' ) ); ?>

  • smallest(integer) (optional) The text size of the tag with the smallest count value (units given by unit parameter).Default: 8
  • largest(integer) (optional) The text size of the tag with the highest count value (units given by the unit parameter).Default: 22
  • unit(string) (optional) Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %.Default: ‘pt’
  • number(integer) (optional) The number of actual tags to display in the cloud. (Use ‘0’ to display all tags.)Default: 45
  • format(string) (optional) Format of the cloud display.
    • ‘flat’ separated by whitespace defined by ‘separator’ parameter.
    • ‘list’ UL with a class of ‘wp-tag-cloud’
    • ‘array’ returns the tag cloud as an array for use in PHP.
    • Default: flat
  • separator(string) (optional) The text/space between tags.Default: ‘\n’ (whitespace)orderby(string) (optional) Order of the tags.
    • ‘name’
    • ‘count’
    • Default: name
  • order(string) (optional) Sort order.
    • ‘ASC’
    • ‘DESC’
    • ‘RAND’ tags are in a random order.
    • Default: ASC
  • exclude(string) (optional) Comma separated list of tags (term_id) to exclude. For example, exclude=5,27 means tags that have the term_id 5 or 27 will NOT be displayed. Defaults to exclude nothing. Default: null
  • include(string) (optional) Comma separated list of tags (term_id) to include. For example, include=5,27 means tags that have the term_id 5 or 27 will be the only tags displayed. Defaults to include everything. Default: null
  • topic_count_text_callback(string) (optional) The function, which, given the count of the posts with that tag, returns a text for the tooltip of the tag link. Default: default_topic_count_text
  • link(string) (optional) Set link to allow edit of a particular tag.
    • ‘view’
    • ‘edit’
    • Default: view
  • taxonomy(string or array) (optional) Taxonomy or array of taxonomies to use in generating the cloud.
    • ‘post_tag’
    • ‘category’
    • ‘link_category’
    • ‘any other registered taxonomy’
    • or array of taxonomies Note: this parameter was introduced with v3.1 of WordPress
  • Default: post_tagecho(boolean) (optional) Display the result or return it in a variable. The default is true (display the tag cloud).
    • 1 (true)
    • 0 (false)
    • Default: true

Example CSS to style the built-in

.tagcloud a{
display:block;
float:left;
padding:4px 10px;
margin-right:7px;
margin-bottom:7px;
background:#EEE;
color:#666;
font-size:11px;
font-weight:bold;
text-transform:lowercase;
text-shadow:1px 1px 1px #FFF;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border:1px solid #CCC;
text-decoration:none;
}
.tagcloud a:hover{
background:#F3F3F3;
color:#000;
text-decoration:none;
}

Example to add to a theme’s function file

function wpb_tag_cloud() {
$tags = get_tags();
$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('wpb_popular_tags', 'wpb_tag_cloud');

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

Example for plugin creation. For reference on plugin creation see this link.

/*
* Plugin Name: Tags for a Category Shortcode
* Plugin URI: http://www.wizzud.com/
* Description: Given a category - slug or id - returns linked tag names for any tag found on a post in that category
* Version: 1.0.0
* Author: Roger Barrett
* Author URI: http://www.wizzud.com/
* License: GPL2+
*/
add_shortcode( 'categorytags', 'tfc_tags_for_category_shortcode' );
function tfc_tags_for_category_shortcode( $atts, $content, $tag ){
  //example : [categorytags cat="sport-feature"/]
  $inst = shortcode_atts(
    array(
      'cat' => ''
    ),
    (array)$atts,
    $tag
  );
  $html = '';
  //no category, no results...
  if( !empty( $inst['cat'] ) ){
    //get the category...
    if( is_numeric( $inst['cat'] ) ){
      $cat = get_term_by( 'id', intval( $inst['cat'] ), 'category' );
    }else{
      $cat = get_term_by( 'slug', $inst['cat'], 'category' );
    }
    if( !empty( $cat ) ){
      //get the posts in the category...
      $postids = get_objects_in_term( $cat->term_id, 'category' );
      if( !is_wp_error( $postids ) && !empty( $postids ) ){
        //get the tags for the posts...
        $tags = wp_get_object_terms( (array)$postids, 'post_tag' );
        if( !is_wp_error( $tags ) && !empty( $tags ) ){
          //make a link for each tag...
          foreach( $tags as $tag ){
            //simple paragraph containing linked tag name...
            $html .= '<p><a href="' . get_term_link( $tag, 'post_tag' ) . '">' . $tag->name . '</a></p>';
          }
        }
      }
    }
  }
  return $html;
}

Example usage : [categorytags cat=”a-category-slug”/]
It will take either a slug or an id.

Example using SQL

function get_category_tags($args)
{
global $wpdb;
$tags = $wpdb->get_results ("
SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
FROM
wp_posts as p1
LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
wp_posts as p2
LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
WHERE
t1.taxonomy = 'category' AND
p1.post_status = 'publish' AND
terms1.term_id IN (".$args['categories'].") AND
t2.taxonomy = 'post_tag' AND
p2.post_status = 'publish' AND
p1.ID = p2.ID
ORDER by tag_name ");
$count = 0;
foreach ($tags as $tag) {
$tags[$count]->tag_link = get_tag_link($tag->tag_id);
$count++;
}
return $tags;
}

Use this in your theme to display
$args = array('categories' => '12,13,14');$tags = get_category_tags($args)

NOTE:
Replace
t2.taxonomy = 'post_tag' AND

With
t2.taxonomy = ".$args['taxonomy']." AND

Then you can use this to find taxonomy terms related to the category.
$args = array('categories' => '12,13,14', 'taxonomy' => 'string');

Where "string is a comma separated list of taxonomy names

Another Example in SQL

function get_category_tags($args) {
global $wpdb;
$tags = $wpdb->get_results("
SELECT DISTINCT
terms2.term_id as tag_ID,
terms2.name as tag_name,
t2.count as posts_with_tag
FROM
$wpdb->posts as p1
LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
$wpdb->posts as p2
LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
WHERE (
t1.taxonomy = 'category' AND
p1.post_status = 'publish' AND
terms1.term_id = '".$args['categories']."' AND
t2.taxonomy = 'post_tag' AND
p2.post_status = 'publish' AND
p1.ID = p2.ID
)
");
$count = 0;
foreach ($tags as $tag) {
$tags[$count]->tag_link = get_tag_link($tag->tag_id);
$count++;
}
return $tags;
}

Use it as such:

$args = array('categories' => '6');
$tags = get_category_tags($args);
$content .= "";
foreach ($tags as $tag) {
$content .= "<a>tag_link">$tag->tag_name</a>";
}
$content .= "";
echo $content;

Reference

Did you get a clue?

If you got a clue and want to thank me, then visit the thank me page. It’s the best way to keep me publishing articles and keeping this site operation.

This site uses affiliate links. When you go to another site from here the link typically will have an affiliate code attached to it. Your actions on that site may earn a small commission for me. Read our affiliate link policy for more details.

{fin}

Scroll to Top