Detailed Explanation of wordpress Custom Tag Cloud and Random Tag Acquisition Method

  • 2021-12-04 09:34:06
  • OfStack

wp_tag_cloud() The function is used to label the cloud, and attributes such as font size and label sorting can be defined according to the number of articles associated with each label. Since version 2.8, the taxonomy (taxonomy) parameter has been added, which means that in addition to the label (tags), taxonomy (Categories) or other custom taxonomy (Custom Taxonomies) can be displayed as a "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 wp_tag_cloud Array (Key), but because this function uses the wp_parse_args() And get_terms() , you can pass get_terms() Use all array keys.

Output by default:

smallest--Minimum tag (least used) display size 8 largest-Maximum tag (most used) display size 22 unit-Maximum and Minimum in 'pt' number--Display up to 45 labels format-Displays all labels in flat form (separated by spaces) separator--Displays spaces between labels orderby--Sort tags by name order-in ascending order exclude-No tags excluded include-including all labels topic_count_text_callback--Use the function default_topic_count_text link-Visual taxonomy--Using article tags as a cloud base echo-Output

However, because this method collects styles into it, it is not very friendly to use. If you want to customize reading tags and modify display styles, it is also very simple. Look at the code example, which is obtained 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 label displayed on the home page, you can use the following code, but this approach seems to be unfavorable to seo, you can use it carefully


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

Use to get random tags get_tags How to change the parameters of the function can't be obtained (anyway, I can't get it, welcome the great god to leave a message for guidance), and finally the sql connection table query was used.

Summarize


Related articles: