【问题标题】:Get all tags based on specific category (including all tags from child categories and posts) wordpress获取基于特定类别的所有标签(包括来自子类别和帖子的所有标签)wordpress
【发布时间】:2014-05-12 07:30:47
【问题描述】:

我想在 single.php 上使用 wp_tag_cloud() 使用参数来获取来自特定类别的所有标签,包括来自其子类别和帖子的所有标签。

【问题讨论】:

  • 顺便说一句,这是一个很好的问题。

标签: wordpress tags


【解决方案1】:

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);

【讨论】:

  • 我意识到我的回答跑题了。这将返回所有标签......但不返回标签云。让我在这方面工作。
  • 添加了第二个函数来输出标签云。它没有很多功能,但可以根据自己的喜好轻松扩展/修改。
  • 我非常感谢您的工作和时间。我测试了您的代码,但不幸的是这不是我所要求的。它在站点范围内呼应所有标签。
  • 你确定吗?它应该只回显所选类别中的帖子当前正在使用的标签。
  • 我刚刚再次测试,它只输出指定类别内的帖子正在使用的标签,而不是网站上的所有标签。这不是你想要的吗?
【解决方案2】:

在你的主题的functions.php中插入以下函数:

function get_category_tags($args) {
    global $wpdb;
    $tags = $wpdb->get_results
    ("
        SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
        FROM
            wp_posts as p1
            LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
            LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
            LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,

            wp_posts as p2
            LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
            LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
            LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
        WHERE
            t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
            t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
            AND p1.ID = p2.ID
        ORDER by tag_name
    ");
    $count = 0;
    foreach ($tags as $tag) {
        $tags[$count]->tag_link = get_tag_link($tag->tag_id);
        $count++;
    }
    return $tags;
}

在您的主题文档中调用该函数,如下所示。请注意,它接受多个类别 ID:

 $args = array(
        'categories'                => '12,13,14'
    );

$tags = get_category_tags($args);

这将返回一个数组,您可以使用它执行以下操作:

$content .= "<ul>";
foreach ($tags as $tag) {
    $content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content;

【讨论】:

  • 很好的查询,绝对比循环遍历所有产品要快。
  • 好决定。但请不要对表格前缀进行硬编码。使用 {$wpdb->prefix}_posts 代替 wp_posts 与其他表相同
【解决方案3】:

我越来越接近这个了。

<div class="tag_cloud_on_single">

    <h2>Popular Topics</h2>

    <?php

    $category = get_the_category();
    $root_cat_of_curr =  $category[0]->category_parent;

    function get_cat_slug($cat_id) {
        $cat_id = (int) $cat_id;
        $category = &get_category($cat_id);
        return $category->slug;
    }

    $my_cat = get_cat_slug($root_cat_of_curr);

    $custom_query = new WP_Query('posts_per_page=-1&category_name='.$my_cat.'');
    if ($custom_query->have_posts()) :
        while ($custom_query->have_posts()) : $custom_query->the_post();
            $posttags = get_the_tags();
            if ($posttags) {
                foreach($posttags as $tag) {
                    $all_tags[] = $tag->term_id;
                }
            }
        endwhile;
    endif;

    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);

    $args = array(
        'smallest'                  => 12, 
        'largest'                   => 24,
        'unit'                      => 'pt', 
        'number'                    => 0,  
        'format'                    => 'flat',
        'separator'                 => "&nbsp;&nbsp;&nbsp;",
        'orderby'                   => 'name', 
        'order'                     => 'RAND',
        'exclude'                   => null,  
        'topic_count_text_callback' => default_topic_count_text,
        'link'                      => 'view', 
        'echo'                      => true,
        'include'                   => $tags_str
    );

    wp_tag_cloud($args);

    ?>

</div>

感谢大家的贡献。感谢您的帮助。

【讨论】:

  • 啊,之前没有意识到有“包含”参数。放入指定标签的智能移动。
猜你喜欢
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多