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

相关推荐
代龙涛15 小时前
WordPress header.php 与 footer.php 模板结构详解
开发语言·php·wordpress
Web极客码6 天前
如何在WordPress中添加动态内容
服务器·前端·wordpress
tommyjiatong9 天前
WordPress电商展示站服务器迁移指南
服务器·数据库·wordpress
IT大白鼠10 天前
WordPress组合漏洞wp2shell技术分析报告
wordpress·安全漏洞·wp2shell 漏洞
Markk13 天前
Wordpress及第三方支持工具docker容器化搭建
wordpress
Web极客码17 天前
WordPress SEO优化:提升网站排名的13个关键步骤
服务器·seo·wordpress
tommyjiatong23 天前
解决 WordPress 必须带 index.php 才能访问及无法提交站点地图(Sitemap)的底层原理与方案
php·网站搭建·wordpress
Web极客码1 个月前
如何在WordPress中通知用户他们的评论已获批准?
服务器·wordpress·评论
钟智强1 个月前
我警告了 329 天
git·php·wordpress·webshell
JokySue1 个月前
外名网深度评测:ICANN认证域名注册商,600多种域名后缀一站搞定
云计算·wordpress·云服务器·域名注册·无代码建站