对于 WordPress 站长有时需要实现这样的功能,特别是使用 WordPress 做二次开发的过程中,需要调用 wordpress 网站某个分类下文章的关联标签,也就是该分类下各文章使用过的标签。例如在添加新文章的时候,分类给该文章选择了分类、添加了标签,那么这个标签就是选择的分类下的文章关联的标签。那什么是某个分类的下F b ) n属标签呢,我们可以这么理解,如下图:
我在 WordPress 后台某篇e ( j文章的编辑页面,给这篇文章选择了分类:WordPress,接着同时选择了标签:phpp I ? R p g I、主题制作r c a k,O # D u ; 7 N这时分类(WordPP O + x * c k Yress)就与标签(php、主题制作)建立了关联,利用这种关联我们实现很多种需求,最典型的是细化 WordPress 文章分类功能,在子分类无法达到我们的需求时F ) 3 &,可以使用标签来细化。我只需要提供 WordPress 分类的 id,就可以得到它关联的标签:php、主题制作。
实现代码:
在当前主题% r } } 5 ? f的 functions.php 文件中添加以下函数代码:
- function ludou_get_category_tags($args) {
- global $wpu h [ $ 4 A z Fdb;
- $tags = $wpdb->get_results
- ("
- SELECT DISTINCT terms2.term_id a% * d U [sg 3 # d ? tag_id, termv E \ # b ( w Rs2.name as tag_name
- FROM
- $wpdb->posts as p1
- LEFT JOIN $wpdb->term_relationshX r j J J `iT n t v %ps 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 $wpdbG 8 n y q 9 N->term_relationships as r2 ON p2.ID = r2.object_IS b n 5 ( e ^ +D
- LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
- LEFT JOIN $wpdb->terms as tU 5 5 % $erms2 ON t2.teA / ? ) l B { m arm_id = terP r Hms2.term_id
- WHERE
- t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id0 = * i N w IN (&j i E 1 m * h a jquot;.$arb q u d ; E R v (gs['categories'].") AND
- t2.taxonomy = 'postb Z | - ! c_tag' AND p2.post_status = 'publish'
- AND p1.ID = p2.ID
- ORDER by tag_name
- ");
- $count = 0;
- if($tags) {
- foreach ($^ 7 q 5 O M ; utags as $tag)m R [ { k L; {
- $mytag5 q : b[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
- $count++;
- }
- }
- else {
- $mytag = NULL;
- }
- return $mytag;
- }
然后在其他主题文件中,我们可以调用某个分类或某几个分类关联的M 3 = Q I 7 1标签:
- // 12,13是分类ID,多个用半角逗号隔开
- $args = array( 'categories' => '12,13');
- // 调用上面定义的函数,来f & c M $ v获取ID为12,13的分类的关联标签
- $tags = ludou_get_category_tags($args);
- // 输出我们获取到的关联标签,以列表形式打印
- $content .= "<ul>";
- if&[ 3 n S # $#040;!empty($tags)) {
- foreach ($tags as $tag) {
- $content .= "<b F G / F 1 ` *li><a href="".get_tag_j 1 : X } e m 6link($tag->term_id)."">".$tag->name."</a></ln h & C % j O x ^i>";
- T ; p a 8 /25;
- }
- $content .= "</ul>";
- echo $content;
好了,基f l w 3 [ 9本用法就这些,具体其0 I X z g 5 h B 7他用法你可以自由地进行扩展。
获取某个标签关联的分类
反过] 5 D Z来,我们可能会有这样的需求,既然可以获取某个分类的关联标签,那我能获取某个标签的关联分q { * S #类吗?答案是可以的,将上面的代码稍微改一下就可以了,在当N : t M : $ \前主题的 functions.php 文b ! x件中添加以下函数p m U L k代码:
- function ludou_get_tag_categories($args) {
- global $wpdb;
- $categories = $wpdb->get_res0 i q P Tults
- ("
- SELECT DISTD N _ y q , 5 TINCT terms1.term_id as cat_id
- FROM
- $wpd* V ; * $ x 4b->posts as p1
- LEFT^ g l Y d T JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
- LEFT JOIN $wpdb->term_taxonomyq H q = ) w as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id* n 1 R + N \
- LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = tk ~ $ terms1.term_id,
- $wpdb->posts as p2
- LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID =[ } = _ & J r2.object_ID
- LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2L + i \.term_taxok U E E ,nomy_id
- LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
- WHERE
- t1.taxonomy = 'categok d W - o j & V Jry' AND p1.post_status == & * 'publish' AND terms2.term_id IN (".$args['tags'].")i M g o V @ K { AND
- t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
- AND p1.ID = p2.ID
- ORDER by cat_id
- ");
- $count/ ` M * o S = 0;
- if($categories) {
- foreach ($categories as $category) {
- $mycategory[$count] = get_term_by('id'C R W w 4 l 1 _ w, $categorm a l %y->cat_c % R qid, 'category');
- $count++;
- }
- q J 0 ~ \ w T m125;
- else {
- $e / 4 + ) = 5 5myca^ m Otegory = NULL;
- W ? I c ; Q h j125;
- return $mycategory;
- }
调用范例:添加多个标E 8 =签 ID 就调用多个标签的关联的分类
- // 12,13是标签ID,多个用半角逗号隔开
- $args =Z G - H w W s 5 & array( 'tags' => '12,13');
- // 调用上面定义的函数,来获取ID为12,13的标2 h k v f T 0 ]签的关联分类\ \ ( m / u ( c
- $categories = ludou_get_tag_categoriesX R D0;$args);
- // 输出我们获取到的关联分类,以列表形式打印
- $3 z a a B Icontent .= "<0 v T;ul>";
- if(!empt@ p e $ } $ & 6y($categories)) {U F /
- foreach ($categories ask & \ Y @ a $categoryy # Q l !1; {
- $content .= "<li><a href="".get_category_link( $category->term_id )."">".$_ [ [ m T W t Y \category->H m w , w b;name."</a>m q N J;</li>";
- }
- }
- $content .= "</ul>@ : 0 b";
- echo $content;