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前端圈1 天前
WordPress报502错误问题解决-php-fpm-84.service loaded failed failed LSB: starts php-fpm
开发语言·nginx·php·wordpress
wodrpress资源分享2 天前
为wordpress自定义一个留言表单并可以在后台进行管理的实现方法
wordpress
林润庚7 天前
【Mark】记录用宝塔+Nginx+worldpress+域名遇到的跨域,301,127.0.0.1,CSS加载失败问题
运维·前端·css·nginx·阿里云·腾讯云·wordpress
wodrpress资源分享7 天前
WordPress多语言插件GTranslate
wordpress
wodrpress资源分享8 天前
给wordpress仪表盘添加自定义图标
wordpress
wodrpress资源分享8 天前
开源程序wordpress在海外品牌推广中的重要作用
wordpress
SmallBambooCode11 天前
【WordPress】发布文章时自动通过机器人推送到钉钉
机器人·php·钉钉·博客·wordpress
Web极客码11 天前
如何在WordPress网站中查看移动版本—快速预览与自定义设置
wordpress·移动端·手机端
still13 天前
WordPress Elementor提示错误无法保存500的解决指南
wordpress