問題描述
我正在尋找在我的帖子下添加標籤雲,只顯示與帖子相關的標籤。顯示的標籤應為標籤雲格式; 具有更多帖子標籤的帖子標籤將以更大的字體顯示。
我已經嘗試在我的帖子下添加以下代碼:
<?php wp_tag_cloud( array(
'smallest' => 8, // font size for the least used tag
'largest' => 22, // font size for the most used tag
'unit' => 'px', // font sizing choice (pt, em, px, etc)
'number' => 45, // maximum number of tags to show
'format' => 'flat', // flat, list, or array. flat = spaces between; list = in li tags; array = does not echo results, returns array
'orderby' => 'name', // name = alphabetical by name; count = by popularity
'order' => 'ASC', // starting from A, or starting from highest count
'include' => $post_id, // ID's of tags to include, displays none except these
'link' => 'view', // view = links to tag view; edit = link to edit tag
'taxonomy' => 'post_tag', // post_tag, link_category, category - create tag clouds of any of these things
'echo' => true // set to false to return an array, not echo it
) ); ?>
我試圖使用 include 數組來調用 post id 來引用 post 標籤。但它不行。它顯示了現有的所有標籤,而不是特定於該帖子的標籤。
有人有解決方案嗎?請幫忙。
最佳解決方案
首先,您需要通過調用 wp_get_post_tags 獲取所有分配的標籤 ID:s,因為 wp_tag_cloud 中的 include 參數僅適用於標籤 id,而不是頁面 ID 。所以當你有所有的 id:s 把它們放在 wp_tag_cloud 中的 include 參數中,就像這樣:
<?php
// Get the assigned tag_id
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
// Check if there is any tag_ids, if print wp_tag_cloud
if ( $tag_ids ) {
wp_tag_cloud( array(
'unit' => 'px', // font sizing choice (pt, em, px, etc)
'include' => $tag_ids, // ID's of tags to include, displays none except these
) );
}
?>
我還刪除了一些與 defaults 不同的參數,只需要修改數組即可添加自定義參數。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。