为我们的文章添上点睛之笔,若能令标签云随机绽放出斑斓色彩,无疑将个性化魅力倍增。如今,我们摒弃繁琐插件,亲力亲为,以代码编织独一无二的彩色标签云。
教程指引
请将以下代码巧妙地融入您子主题的functions.php文件中,其路径为:您的域名/wp-content/themes/ripro/functions.php。
代码展示
在此,我们精选了一款既简洁又高效的代码示例,当然,您也可以在网络海洋中探寻更多选择。只需将这段代码优雅地安置在子主题 functions.php 的尾声处,即可轻松实现。
# 汇站网
# https://www.huizhanii.com/76.html
<span class="token comment">/***圆角背景色标签 - ZTmbk.com***/</span>
<span class="token keyword keyword-function">function</span> <span class="token function">colorCloud</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$text</span> <span class="token operator">=</span> <span class="token function">preg_replace_callback</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'|<a (.+?)>|i'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'colorCloudCallback'</span><span class="token punctuation">,</span> <span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword keyword-return">return</span> <span class="token variable">$text</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword keyword-function">function</span> <span class="token function">colorCloudCallback</span><span class="token punctuation">(</span><span class="token variable">$matches</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$text</span> <span class="token operator">=</span> <span class="token variable">$matches</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$colors</span> <span class="token operator">=</span> <span class="token keyword keyword-array">array</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'48D1CC'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'a26ff9'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'fb7da9'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'666'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'19B5FE'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'ff5e5c'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'ffbb50'</span><span class="token punctuation">,</span><span class="token single-quoted-string string">'1ac756'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$color</span><span class="token operator">=</span><span class="token variable">$colors</span><span class="token punctuation">[</span><span class="token function">dechex</span><span class="token punctuation">(</span><span class="token function">rand</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$pattern</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'/style=(\'|\")(.*)(\'|\")/i'</span><span class="token punctuation">;</span>
<span class="token variable">$text</span> <span class="token operator">=</span> <span class="token function">preg_replace</span><span class="token punctuation">(</span><span class="token variable">$pattern</span><span class="token punctuation">,</span> <span class="token double-quoted-string string">"style=\"display: inline-block; *display: inline; *zoom: 1; color: #fff; padding: 6px 6px; margin: 0 5px 5px 0; background-color: #<span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$color</span><span class="token punctuation">}</span></span>; border-radius: 5px; -webkit-transition: background-color .4s linear; -moz-transition: background-color .4s linear; transition: background-color .4s linear;\""</span><span class="token punctuation">,</span> <span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$pattern</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'/style=(\'|\")(.*)(\'|\")/i'</span><span class="token punctuation">;</span>
<span class="token keyword keyword-return">return</span> <span class="token double-quoted-string string">"<a <span class="token interpolation"><span class="token variable">$text</span></span>>"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'wp_tag_cloud'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'colorCloud'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
转载请注明:汇站网 » 【Ripro 美化】圆角标签云的彩色背景效果实现纯粹的代码