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

相关推荐
longze_711 天前
wordpress上传图片无法显示
wordpress
Web极客码11 天前
解决WordPress后台“外观”菜单消失
linux·服务器·wordpress
globaldomain12 天前
立海世纪:WordPress 6.9的新功能、新模块、新API
前端·javascript·html·新媒体运营·网站建设·wordpress·域名注册
podoor13 天前
专业外贸网站建站公司
wordpress
podoor13 天前
php版本升级后page页面别名调用出错解决方法
开发语言·php·wordpress
Web极客码13 天前
WordPress 被重定向到垃圾站的排查全过程
运维·服务器·网络·wordpress
longze_714 天前
解决wordpress内网穿透后,公网无法访问wordpress管理后台wp-admin问题
数据库·wordpress·反向代理
WordPress学习笔记14 天前
WordPress新手建站选SiteGround还是Hostinger
wordpress
WordPress学习笔记14 天前
专业WooCommerce模板一站式服务平台
wordpress
Web极客码14 天前
如何在WordPress中轻松添加阴影框提升网站设计
wordpress