根据分类来调用当前分类下的全部文章标签的方法,这个应用在WordPress模板制作中,可以增加该分类下内容的相关性粘度,更有利于搜索引擎优化,是WordPress网站SEO必备的。

global $wpdb;
$tags = $wpdb->get_results("
    SELECT DISTINCT wp_terms.term_id, wp_terms.name, wp_terms.slug
    FROM {$wpdb->terms} AS wp_terms
    INNER JOIN {$wpdb->term_taxonomy} AS wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
    INNER JOIN {$wpdb->term_relationships} AS wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
    INNER JOIN {$wpdb->posts} AS wp_posts ON wp_term_relationships.object_id = wp_posts.ID
    WHERE wp_term_taxonomy.taxonomy = 'post_tag'
    AND wp_term_taxonomy.term_id IN (
        SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'category' AND term_id = [Current category ID]
    )
    AND wp_posts.post_status = 'publish'
");

第一步:在主题文件function.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;
}

第二步:在主题的分类页添加如下代码

<?php
$cid = get_query_var('cat'); //当前分类id
$top_id = get_category_root_id($cat); //顶级分类
 
if($cid == $top_id){
	//如果是顶级分类
	$typeid = get_category_root_id($cat);
	$terms = get_terms(array('taxonomy' => 'category','parent' => $typeid ));
	$tags = array();
	foreach($terms as $term) {
		$id  = $term->term_id;
		$arg = array( 'categories' => $id);
		$a   = get_category_tags($arg);
		$tags = array_merge((array)$tags,(array)$a);
	}
}else{
	//如果是子分类
	$id = get_query_var('cat');         
	$args = array( 'categories' => $id);
	$tags = get_category_tags($args);
}
   
if(!empty($tags)){                          //如果标签数组不为空
	$key = array_column($tags,'count');    //排序:获取数组中的count
	array_multisort($key,SORT_DESC,$tags); //排序:对数组进行排序
	$i=1;
    foreach($tags as $tag){ if($i>=35) break;  $i++; //35为循环个数 ?>  
	<li><a href="<?php echo get_tag_link($tag->term_id); ?>"> <?php echo $tag->name; ?></a></li>
	
<? } } ?> 
<?  echo $kwords; ?>