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

相关推荐
李纲明5 小时前
WordPress 定时发布不生效或计划任务堆积:先核 WP-Cron 与系统 crontab,再查 Action Scheduler
wordpress·crontab·devdiy·wp-cron·定时发布·建站排障
snysy1 天前
WordPress伪静态404故障排查笔记
android·笔记·wordpress
代龙涛2 天前
WordPress Query 查询机制详解(WP_Query)
开发语言·后端·php·wordpress
李纲明2 天前
WordPress 更新插件后白屏或致命错误:先开 debug.log 与恢复模式,再二分法定位冲突插件
wordpress·白屏·致命错误·恢复模式·插件冲突·devdiy·debug.log
孙琦Ray2 天前
PrivacySafe 发布 57 个免费开源安全脚本,覆盖 139 个漏洞
wordpress·开源安全·漏洞防护·php2shell·privacysafe
李纲明2 天前
WordPress 报建立数据库连接时出错:先核 wp-config 四元组,再查 MySQL 服务与表修复
运维·数据库·mysql·wordpress·排障·devdiy
李纲明2 天前
WordPress 登录后跳回登录页或提示「Cookie 被阻止」:先核站点 URL 与 HTTPS 一致,再查安全插件与 CDN
https·登录·cdn·wordpress·cookie·devdiy·站点url
李纲明2 天前
WordPress 提示「建立数据库连接时出错」:先核 wp-config 与数据库服务,再查表损坏与磁盘配额
数据库·wordpress·devdiy
李纲明2 天前
WordPress 全站 HTTPS 后仍显示「不安全」或锁被划掉:先清混合内容,再核证书链与强制跳转
网络协议·安全·https·wordpress·devdiy·混合内容
李纲明3 天前
WordPress 白屏或提示「出现了严重错误」:先开 WP_DEBUG 抓致命错误,再二分插件与主题
运维·php·debug·wordpress·白屏·插件冲突·devdiy