wp有官方的标签云,每个页面的标签都一样,这样很难满足正常的需要,例如:我们想在当前分类下显示所属的子标签,这个功能就很难用官方标签云实现了,只能我们自己匹配数据库,读取数据了,下面就是wp/wordpress调用当前分类下的子标签方法:

1.funtions.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
        FROM
            $wpdb->posts as p1
            LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
            LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
            $wpdb->posts as p2
            LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
            LEFT JOIN $wpdb->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;
    if($tags) {
        foreach ($tags as $tag) {
            $mytag[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
            $count++;
        }
    } else {
      $mytag = NULL;
    }
    return $mytag;
}

2.在需要出调用:

<?php
    $cat= single_cat_title('', false);
    $args = array( 'categories' => get_cat_ID($cat));
    $tags = get_category_tags($args);
    $content .= "<ul class='cat-tag'>";
    if(!empty($tags)) {
        foreach ($tags as $tag) {
            $content .= "<li><a href=\"".get_tag_link($tag->term_id)."\">".$tag->name."</a></li>";
        }
    }
    $content .= "</ul>";
    echo $content;
?>

这样就能全部输出当前分类下的所有标签了,对所有标签。

当然,有人说,我不想要全部,只需要15个,那就修改调用代码:

<?php
    $cat= single_cat_title('', false);
    $args = array( 'categories' => get_cat_ID($cat));
    $tags = get_category_tags($args);
    $content .= "<ul class='cat-tag'>";
    if(!empty($tags)) {
        foreach ($tags as $key=> $tag) { 
                if($key>15){break;}
                $content .= "<li><a href=\"".get_tag_link($tag->term_id)."\">".$tag->name."</a></li>";   
         }
    }
    $content .= "</ul>";
    echo $content;
?>

最终效果,就看当前主题的分类菜单顶部标签吧