溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在wordpress中自定義標(biāo)簽云與隨機(jī)獲取標(biāo)簽

發(fā)布時(shí)間:2021-05-18 17:09:08 來(lái)源:億速云 閱讀:251 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了怎么在wordpress中自定義標(biāo)簽云與隨機(jī)獲取標(biāo)簽,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

用法

<?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 —— 按名稱為標(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)獲?。?/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)簽在首頁(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;
}

上述內(nèi)容就是怎么在wordpress中自定義標(biāo)簽云與隨機(jī)獲取標(biāo)簽,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI