怎么獲取指定分類文章下的所有標簽 - WordPress教程

    標簽不依賴于類別,它們是分開的。話雖這么說,要使某個特定類別的所有標簽都在使用中,唯一的方法是循環瀏覽該類別的每個帖子并獲取每個帖子的標簽

    WordPress中沒有本地方法可以做到這一點。原因是標簽不依賴于類別,它們是分開的。話雖這么說,要使某個特定類別的所有標簽都在使用中,唯一的方法是循環瀏覽該類別的每個帖子并獲取每個帖子的標簽。

    我已經編寫了一個快速功能來做到這一點。

    (包括子類別和帖子中的所有標簽)

    將此函數放入您的functions.php文件中。

    function get_tags_in_use($category_ID, $type = 'name'){
        // Set up the query for our posts
        $my_posts = new WP_Query(array(
          'cat' => $category_ID, // Your category id
          'posts_per_page' => -1 // All posts from that category
        ));
    
        // Initialize our tag arrays
        $tags_by_id = array();
        $tags_by_name = array();
        $tags_by_slug = array();
    
        // If there are posts in this category, loop through them
        if ($my_posts->have_posts()): while ($my_posts->have_posts()): $my_posts->the_post();
    
          // Get all tags of current post
          $post_tags = wp_get_post_tags($my_posts->post->ID);
    
          // Loop through each tag
          foreach ($post_tags as $tag):
    
            // Set up our tags by id, name, and/or slug
            $tag_id = $tag->term_id;
            $tag_name = $tag->name;
            $tag_slug = $tag->slug;
    
            // Push each tag into our main array if not already in it
            if (!in_array($tag_id, $tags_by_id))
              array_push($tags_by_id, $tag_id);
    
            if (!in_array($tag_name, $tags_by_name))
              array_push($tags_by_name, $tag_name);
    
            if (!in_array($tag_slug, $tags_by_slug))
              array_push($tags_by_slug, $tag_slug);
    
          endforeach;
        endwhile; endif;
    
        // Return value specified
        if ($type == 'id')
            return $tags_by_id;
    
        if ($type == 'name')
            return $tags_by_name;
    
        if ($type == 'slug')
            return $tags_by_slug;
    }

    然后,當您要獲取特定類別的標簽時,請像下面這樣調用此函數:

    // First paramater is the category and the second paramater is how to return the tag (by name, by id, or by slug)
    // Leave second paramater blank to default to name
    
    $tags = get_tags_in_use(59, 'name');

    希望這可以幫助。

    編輯:

    這是您需要與其他功能結合使用的功能:

    function tag_cloud_by_category($category_ID){
        // Get our tag array
        $tags = get_tags_in_use($category_ID, 'id');
    
        // Start our output variable
        echo '<div class="tag-cloud">';
    
        // Cycle through each tag and set it up
        foreach ($tags as $tag):
            // Get our count
            $term = get_term_by('id', $tag, 'post_tag');
            $count = $term->count;
    
            // Get tag name
            $tag_info = get_tag($tag);
            $tag_name = $tag_info->name;
    
            // Get tag link
            $tag_link = get_tag_link($tag);
    
            // Set up our font size based on count
            $size = 8 + $count;
    
            echo '<span style="font-size:'.$size.'px;">';
            echo '<a href="'.$tag_link.'">'.$tag_name.'</a>';
            echo ' </span>';
    
        endforeach;
    
        echo '</div>';
    }

    因此,您可以像下面這樣簡單地使用此功能:

    tag_cloud_by_category($cat_id);
    教程

    怎么用純代碼限制評論間隔時間 - WordPress教程

    2020-11-11 0:25:00

    教程

    首頁打開會自動下載? - WordPress教程

    2020-12-19 7:43:00

    ??
    Npcink上的部份代碼及教程來源于互聯網,僅供網友學習交流,若您喜歡本文可附上原文鏈接隨意轉載。
    無意侵害您的權益,請發送郵件至 1355471563#qq.com 或點擊右側 私信:Muze 反饋,我們將盡快處理。
    0 條回復 A文章作者 M管理員
      暫無討論,說說你的看法吧
    ?
    個人中心
    購物車
    優惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 中文国产成人精品久久一区| 秋霞日韩一区二区三区在线观看 | 丝袜人妻一区二区三区网站| 熟女精品视频一区二区三区| 无遮挡免费一区二区三区| 久久国产精品视频一区| 日本精品少妇一区二区三区| 亚洲av日韩综合一区二区三区| 丰满爆乳一区二区三区| 日韩社区一区二区三区| 欧美日韩国产免费一区二区三区 | 国产在线精品一区二区在线看 | 亚洲综合国产一区二区三区| 春暖花开亚洲性无区一区二区| 亚洲日本一区二区| 韩国精品福利一区二区三区| 人妻无码一区二区三区免费| 亚洲国产成人久久一区久久| 国产一区二区免费在线| 在线|一区二区三区| 国产伦精品一区二区三区不卡| 亚洲午夜电影一区二区三区| 亚洲成AV人片一区二区密柚| 不卡一区二区在线| 丰满人妻一区二区三区免费视频 | 性无码免费一区二区三区在线| 精品亚洲一区二区三区在线观看 | 国产精品一区12p| 无码国产亚洲日韩国精品视频一区二区三区 | 亚洲国产精品一区二区久久| 中文字幕亚洲一区二区va在线| 亚洲片一区二区三区| 一区二区三区免费视频网站| 亚洲一区二区三区在线视频| 波多野结衣中文字幕一区| 亚洲国产高清在线一区二区三区| 国产福利电影一区二区三区,亚洲国模精品一区 | 国产一区二区三区在线免费| 精品理论片一区二区三区| 国产在线精品一区二区在线看| 日韩精品一区二区三区中文字幕 |