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极客码17 小时前
如何修复WordPress数据库
服务器·数据库·wordpress
小白白搭建3 天前
wordperss AI插件:AI图文+视频+长尾关键词自动生成,已内置deepseek、kimi全模型,支持简单一键接入更多自定义API
人工智能·wordpress·ai插件·deepseek·wordprerss插件·wordpress ai插件
设计源14 天前
尝试通过DeepSeek来优化提高WordPress网站的打开速度,还真有效!
wordpress·deepseek
weixin_4530663721 天前
使用阿里云创建公司官网(使用wordpress)
阿里云·wordpress
podoor21 天前
四大wordpress模板站
wordpress
podoor25 天前
快速搭建WordPress网站的主题
wordpress
podoor1 个月前
WP最主题专业的wordpress主题开发
wordpress
fendouweiqian1 个月前
迁移WordPress网站(大文件版本)
wordpress
fendouweiqian1 个月前
wordpress 利用 All-in-One WP Migration全站转移
wordpress
wordpress主题下载1 个月前
精选10个好用的WordPress免费主题
wordpress·wordpress模板·wordpress主题下载