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

相关推荐
Junson14209913 小时前
wordpress网站底层到顶层优化,彻底解决网站加载速度慢的问题
优化·wordpress·速度·加载速度·woocommerce
Web极客码1 天前
WordPress从经典编辑器升级到古腾堡编辑器
运维·编辑器·wordpress
Web极客码2 天前
WordPress博客关键词
服务器·wordpress·网站加速
WordPress学习笔记2 天前
wordpress文章别名不能为纯数字的原因
wordpress
WordPress学习笔记3 天前
wordpress链接的调用方法
wordpress
WordPress学习笔记3 天前
wordpress建站专家和wordpress建站骗子最大的区别
wordpress
gpldock2226 天前
Flutter App Templates Deconstructed: A 2025 Architectural Review
开发语言·javascript·flutter·wordpress
Junson1420997 天前
使用雷池Waf架构搭建woocommerce外贸网站
wordpress·waf·雷池·woocommerce
Web极客码7 天前
WordPress 在哪里存储网站上的图片?
运维·服务器·wordpress
2601_949532848 天前
Psello HTML Template: A Developer‘s Deep-Dive Review and Guide - Download Free
前端·windows·html·seo·wordpress·gpl