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

相关推荐
VIP_CQCRE6 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 SEO 内容站变成长期增长资产
ai·自动化·seo·wordpress·acedatacloud
2601_9657984710 天前
Renovate Theme Setup: Fast Contractor & Construction Site Architecture
theme·wordpress
2601_9657984711 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
2601_9657984714 天前
Boutique Business Consulting WordPress Architecture & SEO Setup
android·theme·wordpress
2601_9657984715 天前
Plastic Surgery Clinic Web Setup: Deploying Rejuvita WordPress
php·theme·wordpress
2601_9657984715 天前
Contractor Web Setup: Deploying Bathrooms & Kitchens Theme Fast
前端·web3·php·wordpress
e6zzseo22 天前
wordpress独立站seo怎么做才有效
seo·wordpress·独立站·内容优化·外链建设
Web极客码24 天前
如何让用户订阅WordPress评论?提升用户互动的有效策略
运维·服务器·wordpress
fobwebs24 天前
Wordpress Xstore 主题安装Elementor Pro 后找不到某些Elements 组件的解决方法
element·wordpress·form·xstore·pro elements·elementor pro
代龙涛25 天前
WordPress sidebar.php 侧边栏开发教程
开发语言·php·wordpress