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

相关推荐
Web极客码22 分钟前
WordPress维护指南
服务器·网络·wordpress
cll_8692418911 天前
在 YITH Wishlist 页面获取真实 Wishlist Link,并自动带入询盘表单(实战教程)
wordpress
cll_8692418911 天前
修复 YITH Compare 只在产品页可用:让 Header 对比按钮在全站任意页面弹出对比表
wordpress
Web极客码5 天前
如何在 WordPress 中设置会员内容:简单两步实现注册用户专属访问
运维·wordpress·网站管理·网站维护
WordPress学习笔记6 天前
经典免费的wordpress模板
wordpress
Web极客码7 天前
修复WordPress“Cookies Are Blocked Due to Unexpected Output”错误的全攻略
wordpress·wordpress主题·网站运维
gpldock2229 天前
The ROI-Driven Agency‘s Secret Weapon: 2024 WordPress Stack for Conversions
wordpress
WordPress学习笔记10 天前
简洁易用适合新手的wordpress主题模板
wordpress
探索宇宙真理.10 天前
WordPress FS注册密码漏洞 | CVE-2025-15001 复现&研究
经验分享·开源·wordpress·安全漏洞
WordPress学习笔记11 天前
给wordpress网站的图片加alt标签
wordpress