WordPress add color tag cloud

Tag cloud is a lot of WordPress theme has a theme element, today about how to add color for your theme tag cloud, including the sidebar call and page calls.

1. Call the tag cloud
We can use the wp_tag_cloud () function to implement the tag cloud call. For example, the following example:

  1. <?php wp_tag_cloud(‘smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC’);?>

Code Comments:
smallest represents the smallest size of the label
largest represents the largest font size
unit = px indicates that the font uses pixel units
number = 0 means that all labels are displayed, and if it is 40, it means 40
orderby = count means the number of articles associated with the label
order = DESC for descending order (ASC for ascending order, DESC for descending order)
For more information on wp_tag_cloud (), please refer to the WordPress documentation wp tag cloud

2. Add a color function
According to the above parameters, you can already call out the tag cloud and add the following code to the last one of the theme’s functions.php?> In front of the color:

  1. //边栏彩色标签
  2. function colorCloud($text) {
  3.     $text = preg_replace_callback(‘|<a (.+?)>|i’,’colorCloudCallback’, $text);
  4.     return $text;
  5. }
  6. function colorCloudCallback($matches) {
  7.     $text = $matches[1];
  8.     $color = dechex(rand(0,16777215));
  9.     $pattern = ‘/style=(‘|”)(.*)(‘|”)/i’;
  10.     $text = preg_replace($pattern“style=”color:#{$color};$2;””$text);
  11.     return “<a $text>”;
  12. }
  13. add_filter(‘wp_tag_cloud’, ‘colorCloud’, 1);

3. Make the tag cloud page
You can take a look at the theme cat tag cloud page:
1) Copy your theme page.php file at the top of the file Add:

  1. <?php
  2. /*
  3. Template Name: Tags
  4. */
  5. ?>

2) Replace the page.php with the following code:

  1. <?php wp_tag_cloud(‘smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC’);?>

3) The page generally does not require comment function, remove the following code in page.php:

  1. <?php if (comments_open()) comments_template( , true ); ?>

4) You can also according to their own needs, delete some of the functions in page.php, and finally save the file as page-tags.php, so that a tag cloud template to do a good job.
5) visit the WP background – page – create a new page, the page name to fill their own, just in the page properties, select the tags template can:

4. Call the tag cloud in the sidebar
You can use the following function call, the specific method of modification, on your own toss the theme:

  1. <?php wp_tag_cloud(‘smallest=12&largest=18&unit=px&number=20’);?>

However, the general production of more standardized WordPress theme, support Widget gadget, you can in the WP background – appearance – gadget to see whether to support the tag cloud gadget.
Note: This article just tells you how to implement the color tag cloud and how to call it. But the specific style, it is necessary to rely on your own through the CSS code to achieve.