You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress自定义分类模板不显示,默认加载index.php问题

解决自定义分类模板(taxonomy-portfolio-category.php)不生效、默认加载index.php的问题

核心原因

你的自定义文章类型(portfolio)和自定义分类(portfolio-category、portfolio_tag)使用了完全相同的rewrite slug(portfolio),导致WordPress路由解析冲突,无法正确识别分类页面,最终 fallback到index.php。同时代码还存在语法错误和重写规则刷新时机不对的问题。

修复步骤及修正后代码

  1. 修改分类的rewrite slug:给分类设置独立的slug,避免和文章类型slug重复
  2. 修正语法错误:清理分类注册代码中多余的换行和空格
  3. 调整重写规则刷新时机:不在init钩子中频繁刷新规则,改为仅在主题/插件激活时执行一次,或手动刷新

修正后的完整代码

// Portfolio Custom Post Type
function portfolio_init()
{
    // 设置Portfolio标签
    $labels = array(
        'name' => 'Portfolio',
        'singular_name' => 'Portfolio',
        'add_new' => 'Add New Portfolio',
        'add_new_item' => 'Add New Portfolio',
        'edit_item' => 'Edit Portfolio',
        'new_item' => 'New Portfolio',
        'all_items' => 'All Portfolio',
        'view_item' => 'View Portfolio',
        'search_items' => 'Search Portfolio',
        'not_found' =>  'No Portfolio Found',
        'not_found_in_trash' => 'No Portfolio found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Portfolio',
    );
    // 注册自定义文章类型
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'publicly_queryable' => true,
        'hierarchical' => false,
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'query_var' => true,
        'show_in_nav_menus' => true,
        'menu_icon' => 'dashicons-layout',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes'
        )
    );
    register_post_type('portfolio', $args);

    // 注册Portfolio分类
    register_taxonomy('portfolio-category', 'portfolio', array(
        'hierarchical' => true,
        'label' => 'Portfolio Category',
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio-category', 'with_front' => false),
    ));

    // 注册Portfolio标签
    register_taxonomy('portfolio_tag', 'portfolio', array(
        'hierarchical' => false,
        'label' => 'Portfolio Tags',
        'show_ui' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array('slug' => 'portfolio-tag', 'with_front' => false),
    ));
}
add_action('init', 'portfolio_init');

// 仅在主题/插件激活时刷新重写规则
function portfolio_flush_rewrite_rules() {
    portfolio_init();
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'portfolio_flush_rewrite_rules');

后续操作

  1. 替换原有代码后,若为主题代码,可切换一次主题再切回;若为插件代码,重新激活插件。或者直接进入WordPress后台「设置-固定链接」页面,点击保存更改手动刷新重写规则。
  2. 确认主题目录下存在taxonomy-portfolio-category.php文件,可基于主题的category.php或archive.php修改模板内容,确保结构符合WordPress模板规范。

内容的提问来源于stack exchange,提问作者user20166122

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:55:19