在WordPress中获取指定分类下的标签

想要获得某个(或某几个)分类下的全部标签吗?下面的方法一定对你有用。

将以下代码粘贴到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;
}

然后你就可以在模板中使用这个函数了:

$args = array(‘categories’ => ‘12,13,14’);
$tags = get_category_tags($args);


标签:  |  | 
将此文分享到:


转载请注明来自风腾网-wordpress企业主题,本文地址:http://fengten.com/3565.html
除非注明,风腾网-wordpress企业主题文章均为原创,转载请注明出处和链接!
友荐云推荐

相关文章

发表评论:

*

*

顶部 友荐云推荐