WordPress进行二次开发的时候,需要调用WordPress网站某个分类下文章的关联标签,也就是该分类下各文章使用过的标签,今天就为大家分享一下WordPress调用分类关联的标签或者标签关联的分类方法。

实现代码:

在当前主题的functions.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;

}

然后在其他主题文件中,我们可以调用某个分类或某几个分类关联的标签:

// 12,13是分类ID,多个用半角逗号隔开

$args = array( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’categories\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ => \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’12,13\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

// 调用上面定义的函数,来获取ID为12,13的分类的关联标签

$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;

好了,基本用法就这些,具体其他用法你可以自由地进行扩展。

获取某个标签关联的分类

反过来,我们可能会有这样的需求,既然可以获取某个分类的关联标签,那我能获取某个标签的关联分类吗?答案是可以的,将上面的代码稍微改一下就可以了,在当前主题的functions.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;

}

调用范例:添加多个标签ID就调用多个标签的关联的分类

// 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;

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部