怎样挪用wordpress以后分类列表的标签

择要:许多时刻我们在分类文章列表接见时,侧边栏的小工具都是显现的一切站点的热点标签,那若是能力显现以后分类列表的标签呢,下面大…

许多时刻我们在分类文章列表接见时,侧边栏的小工具都是显现的一切站点的热点标签,那若是能力显现以后分类列表的标签呢,下面大挖给人人供应一个解决方案,能够显现以后的分类标签,需要在wordpress主题的中心文章funtion内里增加以下的代码。

function 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; }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 function 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 ; }

其次,将下面挪用输出代码,增加到主题archive.php模板恰当地位:

<?php $cat= single_cat_title(”, false); $args = array( ‘categories’ => get_cat_ID($cat)); $tags = get_category_tags($args); $content .= “<ul class=’cat-tag’>”; 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; ?>

1 2 3 4 5 6 7 8 9 10 11 12 13 <?php      $cat = single_cat_title ( , false ) ;      $args = array ( ‘categories’ = > get_cat_ID ( $cat ) ) ;      $tags = get_category_tags ( $args ) ;      $content . = “<ul class=’cat-tag’>” ;      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 ; ?>

末了,再恰当加上款式便可:

.cat-tag{ float: left; width: 100%; } .cat-tag li a{ float: left; margin: 0 5px; }

1 2 3 4 5 6 7 8 . cat tag {      float : left ;      width : 100 % ; } . cat tag li a {      float : left ;      margin : 0 5px ; }
「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!