×

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

优畅主题 Wordpress技术 2016-05-31 22:35 5,472 1 条评论

首先,在主题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模板适当位置:

 $cat= single_cat_title('', false);
 $args = array( 'categories' => get_cat_ID($cat));
 $tags = get_category_tags($args);
 $content .= "

个人感觉放到头部调用函数:

 get_header();

下面比较合适。
最后,再适当加上样式即可:

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

如果本文对你有帮助,你可以扫描右边的二维码打赏,谢谢支持
联系优畅:uctheme#qq.com (#改为@)
微信订阅号:优畅主题(uctheme)
官网淘宝店:http://uctheme.taobao.com
版权声明:版权归 优畅主题 所有,转载请注明出处!
转载请保留链接: https://www.uctheme.com/wordpress/technical/1720.html
谢谢支付宝打赏
谢谢微信打赏

品牌创立:2012-11-18优畅主题

优畅主题成立于2012年11月18日,专业Wordpress导购主题开发商,拥有多年Wordpress主题设计经验,专门为淘宝客和导购客站长量身打造高端赚钱模板,我们注重细节,有着严谨的开发态度,一切从客户角度出发,如果你也喜欢 WordPress导购主题,欢迎和我们一起交流!


8 + 3 = ?

  1. 123961501
    沙发!
    123961501 2016-06-04 上午 9:13 [回复]

    看看您的博客!

切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册


Warning: error_log(/www/wwwroot/www.uctheme.com/wp-content/plugins/spider-analyser/#log/log-3001.txt): failed to open stream: Permission denied in /www/wwwroot/www.uctheme.com/wp-content/plugins/spider-analyser/spider.class.php on line 2900