我们可能会有这样的需求,既然可以获取某个分类的关联标签,那我能获取某个标签的关联分类吗?答案是可以的,看代码:
主函数,放于 主题function.php文件
function ludou_get_tag_categories($args) {
global $wpdb;
$categories = $wpdb->get_results
("
SELECT DISTINCT terms1.term_id as cat_id
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 terms2.term_id IN (".$args['tags'].") AND
t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
AND p1.ID = p2.ID
ORDER by cat_id
");
$count = 0;
if($categories) {
foreach ($categories as $category) {
$mycategory[$count] = get_term_by('id', $category->cat_id, 'category');
$count++;
}
}
else {
$mycategory = NULL;
}
return $mycategory;
}
调用范例:
// 12,13是标签ID,多个用半角逗号隔开
$args = array( 'tags' => '12,13');
// 调用上面定义的函数,来获取ID为12,13的标签的关联分类
$categories = ludou_get_tag_categories($args);
// 输出我们获取到的关联分类,以列表形式打印
$content .= "<ul>";
if(!empty($categories)) {
foreach ($categories as $category) {
$content .= "<li><a href="".get_category_link( $category->term_id )."">".$category->name."</a></li>";
}
}
$content .= "</ul>";
echo $content;
版权声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。
免责声明: 本网站所发布的一切资源均来自于会员发布以及互联网收集,不代表本站立场,仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则一切后果请用户自负;依据WordPress GPL开源许可协议分享,如有侵犯到您的权益,请联系我们。
免责声明: 本网站所发布的一切资源均来自于会员发布以及互联网收集,不代表本站立场,仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则一切后果请用户自负;依据WordPress GPL开源许可协议分享,如有侵犯到您的权益,请联系我们。