Wordpress开发 - 两种方式给你的文章标签加一个类名(class)

写在前面

仿站的进度终于到了index.php页面啦~现在到修改tags了。默认使用获取标签的函数的a标签是没有样式的,但是如果我们想要给他加一个样式呢?
我就在谷歌找啊找啊,终于在 stackoverflow 上学到两种方式来增加一个类名!

教程开始

其实很简单,只要在你的 index.php 页面使用 <?php the_tags('','',''); ?> 这个函数,然后在functions.php页面增加以下代码即可。

注意哦,这里是  ('','','') 的意思是,去除“标签:”这两个字和“,”分隔符。具体看官方文档  https://developer.wordpress.org/reference/functions/the_tags/

第一种

// add custom class to tag blog.guluqiu.cc
add_filter( 'term_links-post_tag', function( array $links ) {
    return preg_replace_callback(
        '|href="[^"]*/([^"]+?)/?"|',
        function( array $matches ) {
            list( $href, $slug ) = $matches;
            return "class=\"具体类名\" {$href}";
        },
        $links
    );
});

第二种

// add custom class to tag blog.guluqiu.cc
function add_class_the_tags($html){
    $postid = get_the_ID();
    $html = str_replace('<a','<a class="具体类名"',$html);
    return $html;
}
add_filter('the_tags','add_class_the_tags');

来自:Wordpress开发 - 两种方式给你的文章标签加一个类名(class) - 咕噜球 - 记录生活记录学习 (guluqiu.cc) 

0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值