php 按照tag分类文章,获取WordPress当前分类文章所有标签

如果需要在分类列表页面,显示当前分类文章中添加的所有标签,方便读者阅读自己喜欢的内容,下面的代码可以帮你实现这个功能。

首先,在主题functions.php模板文件中添加以下函数: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 .= "

  • ";

if(!empty($tags)) {

foreach ($tags as $tag) {

$content .= "

term_id)."\">".$tag->name."";

}

}

$content .= "

";

echo $content;

?>

个人感觉放到头部调用函数:<?php get_header(); ?>

下面比较合适。

最后,再适当加上样式即可:.cat-tag{

float: left;

width: 100%;

}

.cat-tag li a{

float: left;

margin: 0 5px;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值