‘orderby‘ = ‘count‘
wp_tag_cloud() 函数的感化是用来标签云的,可以按照每个标签所关联的文章次数来界说字体巨细、标签排序等属性。从 2.8 版本开始,添加了 分类法(taxonomy)参数,这就意味着,除了 标签(tags)以外,还可以将 分类(Categories) 或其他 自界说分类法(Custom Taxonomies)作为“云”显示。
用法
<?php wp_tag_cloud( $args ); ?>
默认用法
<?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!) ); ?>
注: child_of 不是一个直接的 wp_tag_cloud 数组的键(Key),但由于这个函数使用 wp_parse_args() 和 get_terms() ,你可以通过 get_terms() 使用所有的数组键。
默认情况下的输出内容:
smallest —— 最小的标签(使用次数最少)显示巨细为8
largest ——最大的标签(使用次数最多)显示巨细为22
unit —— 最大值最小值的单位为‘pt‘
number —— 至多显示45个标签
format —— 以平面形式显示所有标签(标签之间用空格离隔)
separator —— 显示标签之间的空格
orderby —— 按名称为标签排序
order —— 以升序摆列
exclude —— 不排除任何标签
include —— 包孕所有标签
topic_count_text_callback —— 使用函数 default_topic_count_text
link —— 可视
taxonomy —— 用文章的标签作为云根本
echo —— 输出功效
但由于该要领把样式调集到了里面,使用起来不怎么友好,如果想自界说读取标签并改削展示样式该怎么做呢,那也长短常简单的,看代码实例,这里按照get_tags来获取:
$html = ‘<ul>‘; 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=http://www.mamicode.com/‘{$tag_link}‘ title=‘{$tag->name} Tag‘ class=‘{$tag->slug}‘ style=‘color:#{$color}‘>"; $html .= "{$tag->name} ({$tag->count})</a></li>"; } $html .= ‘</ul>‘; echo $html;
如果要求随机获取标签在首页显示,那可以使用以下代码,但这种做法貌似倒霉于搜索引擎优化,可得慎重使用
//获取随机标签 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>‘; foreach($related_posts as $tag) { $color = dechex(rand(0,16777215)); $tag_link = get_tag_link($tag->term_id); $html .= "<li><a href=http://www.mamicode.com/‘{$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; }
获取随机标签用get_tags函数怎么变革参数都是没法获取到的(横竖我是获取不到,,欢迎大神留言指导),功效最后就用的sql连接表盘问就搞出来了。
总结
以上就是这篇文章的全部内容了,但愿本文的内容对大家的学习或者事情具有必然的参考学习价值,感谢大家对敏而好学论坛/嗨学网的撑持。如果你想了解更多相关内容请检察下面相关链接
原文地点是:
wordpress自界说标签云与随机获取标签的要领详解_php技巧 - PHP
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/33156.html
- 上一篇:new TypeReferenceMap泛型
- 下一篇:php大文件分片上传插件