今天小編就為大家分享一篇關(guān)于wordpress自定義標(biāo)簽云與隨機(jī)獲取標(biāo)簽的方法詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧。
wp_tag_cloud() 函數(shù)的作用是用來(lái)標(biāo)簽云的,可以根據(jù)每個(gè)標(biāo)簽所關(guān)聯(lián)的文章次數(shù)來(lái)定義字體大小、標(biāo)簽排序等屬性。從 2.8 版本開(kāi)始,添加了 分類(lèi)法(taxonomy)參數(shù),這就意味著,除了 標(biāo)簽(tags)以外,還可以將 分類(lèi)(Categories) 或其他 自定義分類(lèi)法(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() ,你可以通過(guò) 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)簽之間用空格隔開(kāi))
separator —— 顯示標(biāo)簽之間的空格
orderby —— 按名稱(chēng)為標(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é)果
但由于該方法把樣式集合到了里面,使用起來(lái)不怎么友好,如果想自定義讀取標(biāo)簽并修改展示樣式該怎么做呢,那也是非常簡(jiǎn)單的,看代碼實(shí)例,這里根據(jù)get_tags來(lái)獲取:
- $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)簽在首頁(yè)顯示,那可以使用以下代碼,但這種做法貌似不利于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ù)都是沒(méi)法獲取到的(反正我是獲取不到,歡迎大神留言指導(dǎo)),結(jié)果最后就用的sql連接表查詢(xún)就搞出來(lái)了。
新聞熱點(diǎn)
疑難解答
圖片精選