wordpress自定义html文章,WordPress自定义输出文章标签

众所周知,我们可以使用the_tags()函数进行输出一篇文章的所有标签。

但是,开发路上什么都会碰上,例如给a标签添加一个class属性,这是自带的参数所做不到的,那么我们如何解决呢?

下面我们以给输出的a标签添加一个yoowo的class属性作为例子:

首先在functions.php中添加下面的代码:

/* WordPress自定义输出文章标签

* Author: yoowo222

* CreateTime: 2020/9/19 下午10:07:53

* LastEditor: yoowo222

* ModifyTime: 2020/9/19 下午10:07:53

* Description: WordPress custom output article label

*/

function the_tags_yoowo($before = null, $sep = ', ', $after = '')

{

if (null === $before) {

$before = __('Tags: ');

}

$the_tags = get_the_tag_list_yoowo($before, $sep, $after);

if (!is_wp_error($the_tags)) {

echo $the_tags;

}

}

function get_the_tag_list_yoowo($before = '', $sep = '', $after = '', $id = 0)

{

return apply_filters('the_tags', get_the_term_list_yoowo($id, 'post_tag', $before, $sep, $after), $before, $sep, $after, $id);

}

function get_the_term_list_yoowo($id, $taxonomy, $before = '', $sep = '', $after = '')

{

$terms = get_the_terms($id, $taxonomy);

if (is_wp_error($terms)) {

return $terms;

}

if (empty($terms)) {

return false;

}

$links = array();

foreach ($terms as $term) {

$link = get_term_link($term, $taxonomy);

if (is_wp_error($link)) {

return $link;

}

$links[] = '' . $term->name . '';//添加class属性,如果添加其他属性,也在这里更改

}

$term_links = apply_filters("term_links-{$taxonomy}", $links);

return $before . join($sep, $term_links) . $after;

}

添加的地方已经注释了,你也可以随便自定义它,直到达成想要的效果。

最后就是前台调用了,调用方法是:

当然,这支持the_tags()的所有参数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值