调用WordPress当前分类列表标签

很多时候我们在 WordPress 主题分类文章列表访问时,侧边栏的小工具都是显示站点所有的热门标签,那如果才…

很多时候我们在 WordPress 主题分类文章列表访问时,侧边栏的小工具都是显示站点所有的热门标签,那如果才能显示 WordPress 当前分类列表的标签呢,下面创客云给大家提供一个解决方案,可以显示 WordPress 当前的分类标签,需要在 wordpress 主题的核心文章 funtion 里面添加如下的代码:

  1. function get_category_tags($args) {
  2.     global $wpdb;
  3.     $tags = $wpdb->get_results
  4.     ("
  5.         SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name
  6.         FROM
  7.             $wpdb->posts as p1
  8.             LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
  9.             LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
  10.             LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
  11.             $wpdb->posts as p2
  12.             LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
  13.             LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
  14.             LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
  15.         WHERE
  16.             t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
  17.             t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
  18.             AND p1.ID = p2.ID
  19.         ORDER by tag_name
  20.     ");
  21.     $count = 0;
  22.     if($tags) {
  23.         foreach ($tags as $tag) {
  24.             $mytag[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
  25.             $count++;
  26.         }
  27.     } else {
  28.       $mytag = NULL;
  29.     }
  30.     return $mytag;
  31. }

其次,将下面调用输出代码,添加到主题 archive.php 模板适当位置:

  1. <?php
  2.     $cat= single_cat_title('', false);
  3.     $args = array( 'categories' => get_cat_ID($cat));
  4.     $tags = get_category_tags($args);
  5.     $content .= "<ul class='cat-tag'>";
  6.     if(!empty($tags)) {
  7.         foreach ($tags as $tag) {
  8.             $content .= "<li><a href="".get_tag_link($tag->term_id)."">".$tag->name."</a></li>";
  9.         }
  10.     }
  11.     $content .= "</ul>";
  12.     echo $content;
  13. ?>

最后,再适当加上样式即可:

  1. .cat-tag{
  2.     float: left;
  3.     width: 100%;
  4. }
  5. .cat-tag li a{
  6.     float: left;
  7.     margin: 0 5px;
  8. }
类别:WordPress教程

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册