怎么获取指定分类文章下的所有标签 - 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管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索