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

相关推荐
wordpress主题下载13 天前
精选10个好用的WordPress免费主题
wordpress·wordpress模板·wordpress主题下载
wodrpress资源分享13 天前
2025最新3个wordpress好用的主题
wordpress
清@尘14 天前
ripro 主题激活 问题写入授权Token失败,可能无文件写入权限
主题·wordpress
wodrpress资源分享14 天前
简洁、实用、无插件和更安全为特点的WordPress主题
wordpress
wodrpress资源分享17 天前
wordpress表单插件CF7调用方式
wordpress
侃java19 天前
使用 AsposeWord 向 word 中的文字添加标签
后端·wordpress
wodrpress资源分享23 天前
WordPress调用当前文章作者头像
wordpress
wodrpress资源分享1 个月前
wordpress分类名称调用的几种情况
wordpress
WEB前端圈1 个月前
WordPress报502错误问题解决-php-fpm-84.service loaded failed failed LSB: starts php-fpm
开发语言·nginx·php·wordpress