作为存档页面的CPT分类-错误404

时间:2019-07-16 作者:Pete Hayman

我有一个叫CPT的Clubs. 为了根据俱乐部是否参加同一联赛或杯赛(或两者都不参加)对帖子进行“分类”,我还有一个自定义分类法,名为Opposition.

为了显示哪些俱乐部参加了相同的比赛,我正在尝试创建一个类似于存档页面的页面,其中Clubs 将显示帖子:/clubs/opposition/

为此,我创建了taxonomy-opposition.php 模板文件并编辑functions.php 文件包含以下内容:

// Clubs Post Type
function clubs_init() {
    $labels = array(
        \'name\' => \'Clubs\',
        \'singular_name\' => \'Club\',
        \'add_new\' => \'Add New Club\',
        \'add_new_item\' => \'Add New Club\',
        \'edit_item\' => \'Edit Club\',
        \'new_item\' => \'New Club\',
        \'all_items\' => \'All Clubs\',
        \'view_item\' => \'View Club\',
        \'search_items\' => \'Search Clubs\',
        \'not_found\' =>  \'No Clubs Found\',
        \'not_found_in_trash\' => \'No Clubs found in Trash\', 
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Clubs\',
    );
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => false,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'taxonomies\'  => array( \'clubs\', \'opposition\' ),
        \'query_var\' => true,
        \'rewrite\' => array(
            \'slug\' => \'clubs\',
            \'with_front\' => false
        ),
        \'menu_icon\' => \'dashicons-shield\',
        \'supports\' => array(
        \'title\',
        \'editor\',
        \'excerpt\',
        \'trackbacks\',
        \'custom-fields\',
        \'comments\',
        \'revisions\',                
        \'thumbnail\',
        \'author\',
        \'page-attributes\'
        )
    );
    register_post_type( \'clubs\', $args );
}
add_action( \'init\', \'clubs_init\' );

// Opposition Taxonomy
function opposition_taxonomy() {
    $labels = array(
        \'search_items\'      => \'Search Opposition\',
        \'menu_name\'         => \'Opposition\'
    );
    register_taxonomy( \'opposition\', \'clubs\', 
    array(
        \'hierarchical\' => true,
        \'labels\' => $labels,
        \'query_var\' => true,
        \'show_admin_column\' => true,
        \'has_archive\' => true,
        \'rewrite\' => array(
        \'slug\' => \'clubs\',
        \'with_front\' => false
    ),  
) );
}
add_action( \'init\', \'opposition_taxonomy\' );
尝试访问时的结果/clubs/opposition, 不过,是一个错误404。让我困惑的是,我为我的Players CPT,创建自定义Squad 显示的分类页面。我按照同样的步骤尝试创建Opposition

(我已经冲洗了permalinks)

我已经提到了存在的问题,但到目前为止还没有找到解决方案:

1 个回复
最合适的回答,由SO网友:nmr 整理而成

你用的是同样的子弹clubs 在中注册自定义帖子类型和rewrite 当您注册时opposition 分类学

改变rewrite 中的参数register_taxonomy()

\'rewrite\' => array(
    \'slug\' => \'clubs/opposition\',
    \'with_front\' => false
),
可识别的地址为clubs/opposition/{some-term-name} 并将显示自定义类别中的帖子。

但是clubs/opposition 如果没有其他代码,地址仍然无法工作。

相关推荐