wp_tag_cloud()
函數(shù)的作用是用來標(biāo)簽云的,可以根據(jù)每個(gè)標(biāo)簽所關(guān)聯(lián)的文章次數(shù)來定義字體大小、標(biāo)簽排序等屬性。從 2.8 版本開始,添加了 分類法(taxonomy)參數(shù),這就意味著,除了 標(biāo)簽(tags)以外,還可以將 分類(Categories) 或其他 自定義分類法(Custom Taxonomies)作為“云”顯示。
用法
?php wp_tag_cloud( $args ); ?>
默認(rèn)用法
?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 不是一個(gè)直接的 wp_tag_cloud
數(shù)組的鍵(Key),但由于這個(gè)函數(shù)使用 wp_parse_args()
和 get_terms()
,你可以通過 get_terms()
使用所有的數(shù)組鍵。
默認(rèn)情況下的輸出內(nèi)容:
- smallest —— 最小的標(biāo)簽(使用次數(shù)最少)顯示大小為8
- largest ——最大的標(biāo)簽(使用次數(shù)最多)顯示大小為22
- unit —— 最大值最小值的單位為'pt'
- number —— 至多顯示45個(gè)標(biāo)簽
- format —— 以平面形式顯示所有標(biāo)簽(標(biāo)簽之間用空格隔開)
- separator —— 顯示標(biāo)簽之間的空格
- orderby —— 按名稱為標(biāo)簽排序
- order —— 以升序排列
- exclude —— 不排除任何標(biāo)簽
- include —— 包括所有標(biāo)簽
- topic_count_text_callback —— 使用函數(shù) default_topic_count_text
- link —— 可視
- taxonomy —— 用文章的標(biāo)簽作為云基礎(chǔ)
- echo —— 輸出結(jié)果
但由于該方法把樣式集合到了里面,使用起來不怎么友好,如果想自定義讀取標(biāo)簽并修改展示樣式該怎么做呢,那也是非常簡(jiǎn)單的,看代碼實(shí)例,這里根據(jù)get_tags來獲?。?/p>
$html = 'ul class="post_tags">';
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='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}' style='color:#{$color}'>";
$html .= "{$tag->name} ({$tag->count})/a>/li>";
}
$html .= '/ul>';
echo $html;
如果要求隨機(jī)獲取標(biāo)簽在首頁顯示,那可以使用以下代碼,但這種做法貌似不利于seo,可得慎重使用
//獲取隨機(jī)標(biāo)簽
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 class="post_tags">';
foreach($related_posts as $tag)
{
$color = dechex(rand(0,16777215));
$tag_link = get_tag_link($tag->term_id);
$html .= "li>a href='{$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;
}
獲取隨機(jī)標(biāo)簽用get_tags
函數(shù)怎么變化參數(shù)都是沒法獲取到的(反正我是獲取不到,歡迎大神留言指導(dǎo)),結(jié)果最后就用的sql連接表查詢就搞出來了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
您可能感興趣的文章:- vue實(shí)現(xiàn)標(biāo)簽云效果的方法詳解
- 基于python3生成標(biāo)簽云代碼解析
- 深入解析JS實(shí)現(xiàn)3D標(biāo)簽云的原理與方法
- 如何通過Python實(shí)現(xiàn)標(biāo)簽云算法
- Android實(shí)現(xiàn)隨機(jī)圓形云標(biāo)簽效果
- Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云
- Android TagCloudView云標(biāo)簽的使用方法
- Android實(shí)現(xiàn)3D云標(biāo)簽效果
- Android實(shí)現(xiàn)3D標(biāo)簽云簡(jiǎn)單效果
- Android實(shí)現(xiàn)3D標(biāo)簽云效果
- android隨機(jī)生成圓形云標(biāo)簽效果
- jQuery簡(jiǎn)單實(shí)現(xiàn)彩色云標(biāo)簽效果示例
- javascript實(shí)現(xiàn)動(dòng)態(tài)標(biāo)簽云
- vue實(shí)現(xiàn)標(biāo)簽云效果的示例