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

相关推荐
2501_930786299 天前
WordPress如何绑定多个域名 WordPress实现多域名访问
wordpress
TG_yunshuguoji11 天前
阿里云代理商:轻量应用服务是什么?怎么用轻量应用服务器搭建个人博客?
服务器·阿里云·云计算·wordpress·个人博客
Web极客码12 天前
WordPress.com 和 WordPress.org 之间的区别说明
wordpress
记忆不曾留23 天前
unbuntu 20.04 docker 部署wordpress
运维·docker·容器·wordpress·独立站建站
Web极客码24 天前
如何安装和配置W3 Total Cache以提升WordPress网站性能
wordpress·网站安全·wordpress插件
WordPress学习笔记1 个月前
wordpress的wp-config.php文件的详解
php·wordpress·wp-config
WordPress学习笔记1 个月前
wodpress结构化数据对SEO的作用
wordpress·结构化数据
WordPress学习笔记1 个月前
wordpress登陆前登陆后显示不同的顶部菜单
wordpress
云动雨颤1 个月前
WordPress安全加速:Cloudflare + Nginx缓存优化方案
服务器·wordpress
ZLlllllll01 个月前
常见cms里面的几个cms框架的webshell方法(wordpress,dedecms,phpmyadmin,pageadmin)
cms·wordpress