WordPress 原生的“标签云”区块功能单一,样式呆板(通常只是简单的字号差异)。如果你想要一个大小错落有致、色彩清新、带有微交互动画的标签云页面,使用插件又显得太重。
本文将分享一种 纯代码 (PHP + CSS) 的实现方案。它利用数学算法动态计算标签权重,配合 CSS 的 em 相对单位,实现完美的“整体缩放”和“自然堆叠”效果。
效果
- 大小错落:热门标签不仅字号大,内边距、圆角、间距也会按比例变大,视觉冲击力强。
- 自然云感:放弃死板的网格对齐,采用自然流动布局 + 随机微小旋转,模拟真实的“云朵”形态。
- 完全自动化:基于 Shortcode(短代码)实现,发布文章加标签后自动更新。
第一步:核心逻辑 (functions.php)
我们需要在后端完成两件事:随机打乱顺序(消除首字母排序的呆板感)和 计算字号权重。
注意这里我推荐我们新建一个子主题,然后继承原有主题,不要直接修改原有主题,新建主题请参考这篇文章。
创建好子主题之后,请将以下代码添加到你的子主题的 functions.php 文件末尾:
/**
* === 自定义短代码:灵动大小标签云 [rainbow_tags] ===
* 逻辑:获取标签 -> 打乱顺序 -> 计算最大最小数量 -> 线性插值计算字号
*/
function custom_dynamic_rainbow_cloud() {
// 1. 获取标签列表(按文章数排序,取前60个)
$tags = get_tags(array(
'orderby' => 'count',
'order' => 'DESC',
'number' => 60,
));
if (!$tags) return '';
// 2. 打乱数组顺序,实现“云”的随机分布感
shuffle($tags);
// 3. 获取文章数量的极值,用于权重计算
$counts = array_column($tags, 'count');
$min_count = min($counts);
$max_count = max($counts);
// 4. 设定字号范围 (单位 px)
// 建议拉大差距,这样视觉上的“错落感”更强
$min_size = 12; // 冷门标签大小
$max_size = 28; // 热门标签大小
// 防止除以零错误(当所有标签数量一致时)
$spread = $max_count - $min_count;
if ($spread <= 0) $spread = 1;
// 5. 构建 HTML 输出
$html = '<div class="tag-cloud-container">';
// 标题区域 (可选)
$html .= '<div class="tag-header">';
$html .= '<h2>🏷️ Tag Cloud</h2>';
$html .= '<p>索引知识碎片,探索未知的角落</p>';
$html .= '</div>';
$html .= '<div class="tag-box">';
foreach ($tags as $tag) {
// 核心算法:线性插值计算当前标签的字号
// 公式:MinSize + (当前数量 - Min数量) * (Size差值 / Count差值)
$font_size = $min_size + ($tag->count - $min_count) * ($max_size - $min_size) / $spread;
$link = get_tag_link($tag->term_id);
// 输出 HTML:直接将计算好的 font-size 写入内联样式
$html .= sprintf(
'<a href="%s" class="tag-chip" style="font-size: %.2fpx;">%s <span class="tag-count">%d</span></a>',
esc_url($link),
$font_size,
esc_html($tag->name),
esc_html($tag->count)
);
}
$html .= '</div>'; // close tag-box
$html .= '</div>'; // close container
return $html;
}
// 注册短代码
add_shortcode('rainbow_tags', 'custom_dynamic_rainbow_cloud');
第二步:弹性样式 (CSS)
这是本方案的精髓。我们不再使用固定的 px 来定义边距,而是全部使用 em (相对于当前字体大小)。
这意味着:字号大的标签,它的胶囊背景、内边距、圆角也会自动按比例变大,保持完美的几何比例。
请将以下代码添加到 外观 -> 自定义 -> 额外 CSS:
/* === 标签云 V7:完全弹性缩放 + 随机错落版 === */
/* 1. 外层容器:毛玻璃卡片 */
.tag-cloud-container .tag-box {
background: rgba(255, 255, 255, 0.6);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 20px;
padding: 50px; /* 留足呼吸空间 */
box-shadow: 0 8px 30px rgba(0,0,0,0.04);
/* 布局关键:让标签像文字一样自然流动 */
display: block;
text-align: center;
line-height: 2.5; /* 增加行高,容纳忽大忽小的胶囊 */
max-width: 900px;
margin: 30px auto;
}
/* 手机端适配 */
@media (max-width: 600px) {
.tag-cloud-container .tag-box { padding: 20px; }
}
/* 2. 单个标签胶囊 */
.tag-chip {
display: inline-block; /* 允许设置宽高,且随行流动 */
text-decoration: none !important;
vertical-align: middle; /* 垂直居中对齐 */
/* 【核心魔法】所有尺寸使用 em,实现整体同步缩放 */
padding: 0.35em 0.8em;
border-radius: 0.6em;
margin: 0.3em 0.4em;
/* 基础视觉风格 */
background: #fff;
color: #555;
border: 1px solid rgba(0,0,0,0.08);
font-weight: 700;
transition: all 0.2s cubic-bezier(0.34, 1.56, 0.64, 1); /* 果冻回弹动画 */
box-shadow: 0 0.15em 0.4em rgba(0,0,0,0.03);
position: relative;
}
/* 3. 制造“错落感” (伪随机微调) */
/* 利用 nth-child 给不同位置的标签加一点点随机位移和旋转,打破死板 */
.tag-chip:nth-child(3n+1) { transform: rotate(-1deg) translateY(2px); }
.tag-chip:nth-child(4n+2) { transform: rotate(1deg) translateY(-2px); }
.tag-chip:nth-child(5n+3) { margin-left: 0.6em; } /* 偶尔拉大间距 */
/* 4. 井号装饰 */
.tag-chip::before {
content: '#';
margin-right: 0.2em;
color: #ccc;
font-weight: normal;
font-size: 0.9em;
}
/* 5. 悬停效果 (统一放大) */
.tag-chip:hover {
/* 悬停时归位并放大 */
transform: scale(1.1) rotate(0deg);
background: #ff9a9e; /* 悬停变樱花粉 */
color: #fff !important;
border-color: #ff9a9e;
box-shadow: 0 0.4em 1em rgba(255, 154, 158, 0.4);
z-index: 10;
}
.tag-chip:hover::before { color: rgba(255,255,255,0.7); }
/* 6. 数量角标 - 完美跟随缩放 */
.tag-count {
font-size: 0.6em; /* 永远是当前标签字号的 60% */
position: relative;
top: -0.1em;
background: rgba(0,0,0,0.06);
color: #999;
padding: 0.2em 0.6em;
border-radius: 1em;
margin-left: 0.5em;
font-weight: normal;
transition: all 0.2s;
}
/* 悬停时角标变白 */
.tag-chip:hover .tag-count {
background: rgba(255,255,255,0.3);
color: #fff;
}
第三步:在页面中调用
一切准备就绪,最后一步非常简单。
- 新建一个页面,命名为“标签云”或“Tags”。
- 添加一个 “短代码 (Shortcode)” 区块。
- 输入以下代码:Plaintext
🏷️ Tag Cloud
索引知识碎片,探索未知的角落
教程 13Windows-server 1win7 1centos8 1uptime kuma 1优化 6webp 1短代码 1jdk 1vaultwarden 2docker 1笔记 3主题 1服务器 8caddy 2php 2ssh 1github 1status 1fish 1状态 1硬盘清理 1代码块 1备份 1fuukei 3阿里云oss 1nextcloud 3c语言 1imap 1bitwarden 1mysql 2密钥对生成 1OSS 1c 3centos7 2smtp 2子主题 1https 1css 1安装教程 4网站监控 1wordpress 7插件 1图片压缩 1bug修复 1memories 1 - 保存并发布。

Comments NOTHING