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极客码3 天前
WordPress 站点漏洞利用:数据库恶意注入与多重感染的案例分析
数据库·wordpress·网站安全·数据库注入·wordpress漏洞·wordpress安全插件
wodrpress资源分享14 天前
怎么让二级域名绑定到wordpesss指定的页面
wordpress
wodrpress资源分享18 天前
WordPress用 Options Framework 创建一个自定义相册功能
wordpress
wodrpress资源分享18 天前
经典风格的免费wordpress模板
wordpress
wodrpress资源分享1 个月前
推荐12个wordpress企业网站模板
wordpress
Web极客码1 个月前
在WordPress上添加隐私政策页面
大数据·人工智能·wordpress
wodrpress资源分享1 个月前
wordpress免费主题网站
wordpress
云动雨颤1 个月前
Cloudflare子域名设置指南
网络协议·安全·wordpress
wodrpress资源分享1 个月前
三大中文wordpress原创主题汉主题
wordpress
八戒社1 个月前
如何使用插件和子主题添加WordPress自定义CSS(附:常见错误)
前端·css·tensorflow·wordpress