写主题的时候有的需要给tag自定义clss但是没有具体的方法<?php $this->tags(' ', true, 'none'); ?>会直接输出一个a 连接,自己写样式又麻烦之前都不了了之用别的绕过了这次想把这个功能实现了。

但是找疯了一个都没有找到,后来想到看看别人写的主题有没有实现了这个功能的最终找到了这个侵入性最小的实现

代码如下:

function printTag($that) { ?>
        <?php if (count($that->tags) > 0): ?>
            <?php foreach( $that->tags as $tags): ?>
            <a href="<?php print($tags['permalink']) ?>" class="btn btn-primary btn-sm ml-0 mr-1 mb-1 hover-target"><span><?php print($tags['name']) ?></span></a>
            <?php endforeach;?>
        <?php else: ?>
            <a class="btn btn-primary btn-sm ml-0 mr-1 mb-1 hover-target"><span>无标签</span></a>
        <?php endif;?>
<?php }

在主题functions.php文件内放入上面代码 按需要更改上面的class 啥的。

使用方法:

标签:

<?php printTag($this); ?>

如果不需要给A加样式还可以这样不需要加方法
给每个标签套上div

<div><?php $this->tags('</div><div>', true, 'none'); ?></div>

转自:https://www.krsay.com/typecho/typecho-tag-diy-clss.html