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

相关推荐
李纲明9 天前
WordPress 标签页或附件页被大量收录:先核站点地图与 noindex,再查归档
wordpress·标签页·devdiy·站点地图·noindex·附件页·索引控制
xbzb10 天前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
李纲明10 天前
WordPress 广告落地页与站内不一致或支付物流说明乱:先核对齐信息,再查模板
wordpress·devdiy·广告落地页·物流说明·支付说明·信息对齐·页面模板
李纲明10 天前
WordPress 帮助文档放在另一系统几乎搜不到:先核子目录与反向代理,再查支柱页
wordpress·反向代理·内容中心·帮助文档·devdiy·子目录·支柱页
李纲明12 天前
WordPress 已加结构化数据却不出富结果:先核 JSON-LD 与插件来源,再查必填
schema·wordpress·结构化数据·json-ld·devdiy·富结果·seo插件
李纲明12 天前
WordPress 区块编辑器排好后前台缺块或提示异常:先核区块校验与粘贴来源,再查插件
wordpress·devdiy·gutenberg·区块编辑器·区块校验·异常内容·可重用区块
李纲明13 天前
WordPress 迁移后首页能开、内页 404 或图片裂图:先核伪静态与站点地址,再查媒体
wordpress·404·伪静态·媒体库·devdiy·站点迁移·站点地址
李纲明13 天前
WordPress 外贸主题切语言后版式乱或海外很慢:先核翻译插件适配,再查海外节点与询盘
wordpress·多语言·询盘·devdiy·主题选型·外贸站·wpml
李纲明14 天前
WordPress 询盘跟进不及时或投放渠道对不上:先核通知与 UTM,再查表单插件是否重复
wordpress·utm·devdiy·询盘通知·表单插件·渠道追踪·线索跟进
李纲明14 天前
WordPress 切换语言后仍是原文或页面串语:先核译文关联与页面缓存,再查切换器落地
wordpress·多语言·页面缓存·语言切换·切换器·devdiy·译文关联