wordpress自定the_category的输出结构

通过WordPress的过滤器the_category来自定义输出内容。方法很简单,但是很实用。以下是一个示例代码:

function custom_the_category($thelist, $separator = '', $parents = '') {
    // 获取当前文章的所有分类
    $categories = get_the_category();

    if (empty($categories)) {
        return $thelist;
    }

    $thelist = '';
    foreach ($categories as $category) {
        $cat_name = esc_html($category->name);
        $cat_link = esc_url(get_category_link($category->term_id));

        // 自定义每个分类的HTML结构
        $thelist .= '<div class="custom-category-item">';
        $thelist .= '<a href="' . $cat_link . '" class="custom-category-link">';
        $thelist .= '<span class="custom-category-name">' . $cat_name . '</span>';
        $thelist .= '</a>';
        $thelist .= '</div>';
    }

    return $thelist;
}
add_filter('the_category', 'custom_the_category', 10, 3);

然后在模板文件中,你可以像平常一样调用the_category()函数:

<?php the_category(', '); ?>

 原文

http://www.chudafu.com/jianzhan/6932.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值