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 安全加固基线:更新策略、文件权限、登录面收缩和备份验证(含回滚注意)
运维·安全·php·wordpress
李纲明8 小时前
WordPress 表单提交成功、线索却没到:SMTP、反垃圾与缓存/CDN 冲突怎么拆开查
cdn·wordpress·smtp·表单·网站运维
李纲明3 天前
WordPress 站点变慢:按 TTFB → 缓存 → 查询 → 前端 分层排查(含配置注意)
前端·缓存·性能优化·wordpress·后端开发
程序员老赵3 天前
Docker 部署 WordPress:轻松搭建开源内容管理平台
docker·开源·wordpress
object not found12 天前
WooCommerce 接入 KPay 后订单完成页不显示订单信息:returnUrl 动态参数问题解决
前端·wordpress·独立站支付
皓月盈江15 天前
WordPress博客迁移到Hexo静态博客教程
linux·服务器·hexo·wordpress·博客迁移·静态博客
boke112百科25 天前
WordPress经典编辑器【插入至文章】按钮怎么调到右上角?
编辑器·wordpress·wordpress教程·经典编辑器
Rainy Blue8831 个月前
从零搭建个人动态博客
博客·wordpress
hui-梦苑1 个月前
[PHP]轻量主题函数WordPress 集成 KaTeX 数学公式渲染
开发语言·php·wordpress·katex
VIP_CQCRE1 个月前
用 Ace Data Cloud 把 WordPress 变成你的 AI 内容营销中枢
ai·seo·wordpress·内容营销·ace data cloud