問題描述

我正在尋找在我的帖子下新增標籤雲,只顯示與帖子相關的標籤。顯示的標籤應為標籤雲格式; 具有更多帖子標籤的帖子標籤將以更大的字型顯示。

我已經嘗試在我的帖子下新增以下程式碼:

<?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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。