Products
GG网络技术分享 2025-03-18 16:07 0
WordPress 自带标签云小工具,侧栏直接添加即可,就是默认样式有点平淡,可以加点代码实现彩色标签云及简单优化一下。
WordPress小技巧
WordPress 纯代码实现彩色标签云代码丢到 WordPress 主题 functions.php 文件即可:
//彩色标签云 tag cloudfunction theme_color_cloud_hook($text) {
$text = preg_replace_callback(|<a (.+?)>|i, color_cloud_callback, $text);
return $text;
}
function color_cloud_callback($matches) {
$text = $matches[1];
$color = dechex(rand(0,));
$pattern = /style=(\\|\\\")(.*)(\\|\\\")/i;
$text = preg_replace($pattern, \"style=\\\"color:#{$color};$2;\\\"\", $text);
return \"<a $text>\";
}
add_filter(wp_tag_cloud, theme_color_cloud_hook, 1);WordPress 标签云简单优化配置
代码丢到 WordPress 主题 functions.php 文件即可,按需修改:
/*** 修改自带标签云 widget 字体大小
*/
//wp_tag_cloud 默认参数
//$defaults = array(
//smallest => 8,//标签云中计数最少的标签字体大小,默认值为 8pt
//largest=> 22,//标签云中计数最多的标签字体大小,默认值为 22pt
//unit => pt,//标签文字大小单位,默认为pt,可以为px、em、pt、百分比等
//number => 45,//标签云中显示的最多标签数量,默认值为45个,0 则显示所有标签
//format => flat,//标签输出格式,可选 flat、list 和 array,默认为 flat 平铺,list 为列表方式
//separator=> \"\\n\",//标签之间分割文本/空格。默认值 /n (空格)
//orderby=> name,//标签排序方式,默认值 name 按名称排序,count 则按标签文章数量排序
//order=> ASC,//默认为 ASC 按正序,DESC 倒序,RAND 随机
//exclude=> ,//输入标签ID排除部分标签,如 exclude=1,3,5,7 不显示ID为 1、3、5、7 的标签
//include=> ,//包含标签,与 exclude 用法一样,作用相反。
//link => view,
//taxonomy => post_tag,//默认 post_tag 使用文章标签来生成数据 ;category 则用文章分类生成
//post_type=> ,//文章类型
//echo => true,//默认值 true 打印显示标签云,0 或 false 则返回标签云数据
//show_count => 0,//默认 0 不显示标签文章数量
//);
function twentytwelve_widget_tag_cloud_args( $args ) {
$args[largest]= 18;
$args[smallest] = 6;
$args[unit] = pt;
$args[format] = flat;
return $args;
}
add_filter( widget_tag_cloud_args, twentytwelve_widget_tag_cloud_args );
//标签云 tag 新标签页打开
add_filter(wp_tag_cloud, blank_wp_tag_cloud);
function blank_wp_tag_cloud( $text ) {
return str_replace( <a href=, <a target=\"_blank\" href=, $text );
}关于WordPress纯代码实现彩色标签云及简单优化配置就讲解完了,有任何的问题,都可以私信,同时再为大家分享一家服务器供应商致臻数据(zhizhenyun.com.cn),他家目前拥有香港宽频、漂亮国G口、法国高防OVH3大线路服务器VPS,并有独立面板和几乎全天在线的客服售后,推荐使用,有任何需要指导和测评也欢迎给主机测评&技术分享留言哦!
Demand feedback