当前位置:「千越SEO」 / 网站维护 / WordPress调用指定分类目录下的TAG标签

WordPress调用指定分类目录下的TAG标签

来源:千越SEO
浏览:360

在网站优化过程中可能会需要对已有栏目、页面进行优化调整,比较常见的一种就是“内容聚合”,通过TAG页面来集中某一类型的内容,有时也需要用TAG页面做长尾关键词的优化,所以把某一个栏目下的TAG集中到一起也是很有必要的。

WordPress获取指定分类下的TAG标签

本文主要介绍wordpress如何调用某一分类下的TAG,例如,获取“栏目一”下的所有TAG标签。

1、在functions.php文件中添加以下函数(一般添加到PHP结束标签之前就可以了):

function ludou_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
// 1,2是分类ID,多个用半角逗号隔开
$args = array( 'categories' => '1,2');
$tags = ludou_get_category_tags($args);
// 以列表形式输出
$content .= "<ul>";
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;
?>
本文地址:https://www.seoo.net/maint/625.html
标签:
  • qq
  • 微信咨询
  • 扫一扫,微信咨询