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极客码2 天前
加快WooCommerce性能
服务器·wordpress·网站
豆豆5 天前
WordPress至PageAdmin CMS跨平台迁移技术指南:应对环境约束的系统化过渡方案
cms·wordpress·建站系统·内容管理系统·网站管理系统·pageadmin
Web极客码10 天前
Akismet对WordPress防垃圾评论
服务器·搜索引擎·wordpress
尘中客12 天前
【2026最新】如何用 WordPress 零代码搭建八字排盘/紫微斗数网站(附免费开源插件)
php·api·wordpress·建站源码·网站引流
WordPress学习笔记13 天前
B2B独立站与B2C独立站的主题模板选择的对不对很重要
wordpress·gracetheme·wodetheme
WordPress学习笔记13 天前
一般创业公司官网用WordPress主题模板完全足够
wordpress
lilihuigz14 天前
WordPress 7.0 AI基础设施详解:能力API、AI客户端与MCP适配器如何重塑插件生态
人工智能·wordpress·独立站
CSharp精选营14 天前
2026个人博客建站指南:这4种方案总有一款适合你
.net·wordpress·个人博客·独立站·githubpages·建站教程
WordPress学习笔记15 天前
WodeShop构建下一代高性能电商的核心驱动力
wordpress
WordPress学习笔记16 天前
推荐几个SEO友好的wordpress主题
wordpress