SoFunction
Updated on 2025-03-02

Detailed explanation of wordpress custom tag cloud and random access to tags

wp_tag_cloud() The function is used for tag cloud. It can define font size, tag sorting and other attributes based on the number of articles associated with each tag. Starting from version 2.8, the taxonomy parameter has been added, which means that in addition to tags, Categories or other custom taxonomy can also be displayed as "cloud".

usage

<?php wp_tag_cloud( $args ); ?>

Default usage

<?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!) ); ?>

Note: child_of is not a direct onewp_tag_cloud Key (Key) of the array, but because this function useswp_parse_args() andget_terms() , you can passget_terms() Use all array keys.

The output by default:

  • smallest - The smallest label (minimum number of used) displays size 8
  • largest —The largest label (most used) display size is 22
  • unit — The maximum value is the minimum value and the unit is 'pt'
  • number — display up to 45 tags
  • format — display all labels in flat form (separated by spaces)
  • separator - displays spaces between labels
  • orderby — Sort by name as label
  • order —— Order in ascending order
  • exclude — No tags are excluded
  • include - including all tags
  • topic_count_text_callback ——Use the function default_topic_count_text
  • link ——Visual
  • taxonomy - Use the article's tags as the cloud foundation
  • echo —— Output result

However, since this method collects the styles inside, it is not very friendly to use. If you want to customize reading tags and modify the display style, it is also very simple. Look at the code example, here we get it according to get_tags:

$html = '<ul class="post_tags">';
foreach (get_tags( array('number' => 50, 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => false) ) as $tag){
 $color = dechex(rand(0,16777215));
 $tag_link = get_tag_link($tag->term_id);
 $html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}' style='color:#{$color}'>";
 $html .= "{$tag->name} ({$tag->count})</a></li>";
}
$html .= '</ul>';
echo $html;

If you require random access to the tag to be displayed on the homepage, you can use the following code, but this approach seems to be unfavorable to SEO, so you can use it with caution

//Get random tagsfunction get_rand_tags()
{
 global $post, $wpdb;
 $sql = "SELECT * FROM {$wpdb-&gt;prefix}terms wt INNER JOIN {$wpdb-&gt;prefix}term_taxonomy wtt on wt.term_id=wtt.term_id where ='post_tag' ORDER BY RAND() LIMIT 20";
 $related_posts = $wpdb-&gt;get_results($sql);
 $html = '&lt;ul class="post_tags"&gt;';
 foreach($related_posts as $tag)
 {
 $color = dechex(rand(0,16777215));
 $tag_link = get_tag_link($tag-&gt;term_id);
 $html .= "&lt;li&gt;&lt;a href='{$tag_link}' target='_blank' title='{$tag-&gt;name} Tag' class='{$tag-&gt;slug}' style='color:#{$color}'&gt;";
 $html .= "{$tag-&gt;name} ({$tag-&gt;count})&lt;/a&gt;&lt;/li&gt;";
 }
 $html .= '&lt;/ul&gt;';
 echo $html;
}

Get random tagsget_tagsNo matter how the function changes the parameters, it cannot be obtained (I can't get them anyway, so the master is welcome to leave a message to guide him). As a result, I finally used the SQL connection table query.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links