WordPress添加彩色标签云

前端之家收集整理的这篇文章主要介绍了WordPress添加彩色标签云前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

标签云是很多wordpress主题都有的一个主题元素,上一篇wordpress教程中,我们已经向大家介绍了的方法。很多站长在的过程中,想要给主题添加彩色标签云功能包括边栏调用页面调用,今天我们就向大家介绍一下实现的方法

1.调用标签云

我们可以使用 wp_tag_cloud() 函数实现标签云调用。比如下面的样例:

Crayon-5c891cc245e07970312099" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc245e07970312099-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc245e07970312099-1">Crayon-ta">PHPCrayon-h"> Crayon-e">wp_tag_cloudCrayon-sy">(Crayon-s">'smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC'Crayon-sy">)Crayon-sy">;

代码注释:
smallest表示标签的最小字号
largest表示最大字号
unit=px表示字体使用像素单位
number=0表示显示所有标签,如果为40,表示显示40个
orderby=count表示按照标签所关联的文章数来排列
order=DESC表示降序排序(ASC表示升序排序,DESC表示降序排序)

2. 添加彩色标签云功能

根据上面的参数,您已经可以调用标签云了,切换到主题目录,打开 functions.PHP 文件,加入以下代码即可实现彩色标签云功能

Crayon-5c891cc245e11418149858" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc245e11418149858-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-2">2
Crayon-num" data-line="Crayon-5c891cc245e11418149858-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-4">4
Crayon-num" data-line="Crayon-5c891cc245e11418149858-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-6">6
Crayon-num" data-line="Crayon-5c891cc245e11418149858-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-8">8
Crayon-num" data-line="Crayon-5c891cc245e11418149858-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-10">10
Crayon-num" data-line="Crayon-5c891cc245e11418149858-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e11418149858-12">12
Crayon-num" data-line="Crayon-5c891cc245e11418149858-13">13
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc245e11418149858-1">Crayon-c">//边栏彩色标签
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-2">Crayon-t">functionCrayon-h"> Crayon-e">colorCloudCrayon-sy">(Crayon-v">$textCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cc245e11418149858-3">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">preg_replace_callbackCrayon-sy">(Crayon-s">'||i'Crayon-sy">,Crayon-s">'colorCloudCallback'Crayon-sy">,Crayon-h"> Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-4">Crayon-h"> Crayon-k ">returnCrayon-h"> Crayon-v">$textCrayon-sy">;
Crayon-line" id="Crayon-5c891cc245e11418149858-5">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-6">Crayon-t">functionCrayon-h"> Crayon-e">colorCloudCallbackCrayon-sy">(Crayon-v">$matchesCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cc245e11418149858-7">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-v">$matchesCrayon-sy">[Crayon-cn">1Crayon-sy">]Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-8">Crayon-h"> Crayon-v">$colorCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">dechexCrayon-sy">(Crayon-e">randCrayon-sy">(Crayon-cn">0Crayon-sy">,Crayon-cn">16777215Crayon-sy">)Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891cc245e11418149858-9">Crayon-h"> Crayon-v">$patternCrayon-h"> Crayon-o">=Crayon-h"> Crayon-s">'/style=(\'|\”)(.*)(\'|\”)/i'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-10">Crayon-h"> Crayon-v">$textCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">preg_replaceCrayon-sy">(Crayon-v">$patternCrayon-sy">,Crayon-h"> Crayon-s">"style=\"color:#{$color};$2;\""Crayon-sy">,Crayon-h"> Crayon-v">$textCrayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891cc245e11418149858-11">Crayon-h"> Crayon-k ">returnCrayon-h"> Crayon-s">""Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e11418149858-12">Crayon-sy">}
Crayon-line" id="Crayon-5c891cc245e11418149858-13">Crayon-e">add_filterCrayon-sy">(Crayon-s">'wp_tag_cloud'Crayon-sy">,Crayon-h"> Crayon-s">'colorCloud'Crayon-sy">,Crayon-h"> Crayon-cn">1Crayon-sy">)Crayon-sy">;

3. 制作标签云页面

1) 复制你主题的 page.PHP 文件,在该文件的顶部添加

Crayon-5c891cc245e14788885519" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">page-tags.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc245e14788885519-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e14788885519-2">2
Crayon-num" data-line="Crayon-5c891cc245e14788885519-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cc245e14788885519-4">4
Crayon-num" data-line="Crayon-5c891cc245e14788885519-5">5
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc245e14788885519-1">Crayon-ta">PHP
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e14788885519-2">Crayon-c">/*
Crayon-line" id="Crayon-5c891cc245e14788885519-3">Crayon-c">Template Name: Tags
Crayon-line Crayon-striped-line" id="Crayon-5c891cc245e14788885519-4">Crayon-c">*/
Crayon-line" id="Crayon-5c891cc245e14788885519-5">

2) 使用下面的代码替换page.PHP中的 the_content();

Crayon-5c891cc245e16478849262" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">page-tags.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc245e16478849262-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc245e16478849262-1">Crayon-ta">PHPCrayon-h"> Crayon-e">wp_tag_cloudCrayon-sy">(Crayon-s">'smallest=12&largest=18&unit=px&number=0&orderby=count&order=DESC'Crayon-sy">)Crayon-sy">;

3) 删除page.PHP中的某些不需要的功能,最后将该文件另存为 page-tags.PHP ,这样,一个标签云模板就做好了。

4) 访问 wordpress后台-页面-新建页面页面名称随便填一下,在 页面属性 中,选择 tags 模板即可。

4. 边栏中调用标签云

您可以使用下面的函数调用,具体的修改方法,就靠您自己折腾主题了:

Crayon-5c891cc245e17838471915" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cc245e17838471915-1">1
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cc245e17838471915-1">Crayon-ta">PHPCrayon-h"> Crayon-e">wp_tag_cloudCrayon-sy">(Crayon-s">'smallest=12&largest=18&unit=px&number=20'Crayon-sy">)Crayon-sy">;

注:一般制作比较规范的wordpress主题,都支持 Widget小工具,您可以在 wordpress后台-外观-小工具 中查看是否支持 标签云小工具。

猜你在找的wordpress相关文章