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 中的Alt文本与图像标题区别
wordpress
gplitems1231 天前
Secura – CCTV Security WordPress Theme
seo·wordpress·web design
gplitems1232 天前
ProGuards – Safety Body Guard Security WordPress Theme
seo·wordpress·web design
Web极客码3 天前
如何在WordPress网站中添加Cookie弹窗
前端·安全·github·wordpress
WordPress学习笔记5 天前
免费wordpress主题网站
wordpress
howard20055 天前
利用Docker-Compose部署WordPress
docker-compose·wordpress
WordPress学习笔记8 天前
把二级域名映射到wordpress网站相同别名的page页面
wordpress·二级域名·批量绑定
羑悻的小杀马特10 天前
Docker Compose高手实践之路:简单拓扑、数据库代理、基于WordPress的个人博客站点一键搞定!
数据库·docker·容器·wordpress·docker compose
IT利刃出鞘10 天前
WordPress--将文章的H标签降级
wordpress
Junson14209912 天前
完美修改wordpress网站域名地址(无需登录wordpress后台)
域名·wordpress