WordPress 主题分类页面面包屑导航

风中赏雪 Wordpress Wordpress 2025-09-27 114 0

在主题文件functions.php中添加:


function custom_breadcrumbs() {
    // 首页
    echo '<a href="' . home_url() . '">首页</a>';

    if (is_category()) {
        // 当前分类
        $current_cat = get_queried_object();
        
        // 父级分类
        $ancestors = get_ancestors($current_cat->term_id, 'category');
        if ($ancestors) {
            $ancestors = array_reverse($ancestors);
            foreach ($ancestors as $ancestor) {
                $cat = get_category($ancestor);
                echo ' &raquo; <a href="' . get_category_link($ancestor) . '">' . $cat->name . '</a>';
            }
        }
        
        // 当前分类(无链接)
        echo ' &raquo; <span class="current-category">' . $current_cat->name . '</span>';
    }
}

在分类模板(如category.php)中调用:


<div class="breadcrumb-trail">
    <?php custom_breadcrumbs(); ?>
</div>