自定义邮政类型和分类404

时间:2018-07-27 作者:SamXronn

我已经创建了自定义的帖子类型&;然而,当我转到我的分类页面时,它抛出了一个404错误,我不明白为什么。

// Register Custom Post Type
function portfolio_post_type() {
    register_taxonomy_for_object_type(\'category\',\'portfolio\');
    register_post_type( \'portfolio\',
        array(
            \'labels\' => array(
            \'name\' => __(\'Portfolio\'),
                \'singular_name\' => __(\'Portfolio\'),
                \'add_new\' => __(\'Add new item\'),
                \'add_new_item\' => __(\'Add new item\'),
                \'edit\' => __(\'Edit\'),
                \'edit_item\' => __(\'Edit item\'),
                \'new_item\' => __(\'New item\'),
                \'view\' => __(\'View item\'),
                \'view_item\' => __(\'View item\'),
                \'search_items\' => __(\'Search portfolio\'),
                \'not_found\' => __(\'No portfolio items found\'),
                \'not_found_in_trash\' => __(\'No portfolio items found in trash\'),
            ),
            \'public\' => true,
            \'rewrite\' => array(\'slug\' => \'portfolio\'),
            \'publicly_queryable\' => true,
            \'hierarchical\' => false,
            \'has_archive\' => true,
            \'menu_icon\' => \'dashicons-art\',
            \'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\',\'author\'),
            \'can_export\' => true,
            \'taxonomies\' => array(\'portfolio-category\'),
        )
    );
}
add_action( \'init\', \'portfolio_post_type\', 0 );

function portfolio_create_taxonomy() {
    register_taxonomy(
        \'portfolio-category\',
        \'portfolio\',
        array(
            \'label\' => __( \'Category\' ),
            \'rewrite\' => array( \'slug\' => \'category\' ),
            \'hierarchical\' => true,
        )
    );
}
add_action( \'init\', \'portfolio_create_taxonomy\' );
当我单击查看分类法时http://domain.local/category/all/

然而,它抛出了404页模板。

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

请将“portfolio\\u create\\u taxonomy”功能替换为以下代码

function portfolio_create_taxonomy() {


    register_taxonomy(
        \'portfolio-category\',
        array( \'portfolio\' ),

        array(
        \'hierarchical\'      => true,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
         \'label\' => __( \'Category\' ),
         \'rewrite\' => array( \'slug\' => \'portfolio-category\' ),
         \'hierarchical\' => true,
        )
    );
}
add_action( \'init\', \'portfolio_create_taxonomy\' );

SO网友:nmr

鼻涕虫category 已在使用中。Reserved_Terms

更改后,打开“永久链接设置”页面并单击“保存”。

function portfolio_post_type() {
    //register_taxonomy_for_object_type(\'category\',\'portfolio\');
    register_post_type( \'portfolio\',
        array(
            \'labels\' => array(
            \'name\' => __(\'Portfolio\'),
                \'singular_name\' => __(\'Portfolio\'),
                \'add_new\' => __(\'Add new item\'),
                \'add_new_item\' => __(\'Add new item\'),
                \'edit\' => __(\'Edit\'),
                \'edit_item\' => __(\'Edit item\'),
                \'new_item\' => __(\'New item\'),
                \'view\' => __(\'View item\'),
                \'view_item\' => __(\'View item\'),
                \'search_items\' => __(\'Search portfolio\'),
                \'not_found\' => __(\'No portfolio items found\'),
                \'not_found_in_trash\' => __(\'No portfolio items found in trash\'),
            ),
            \'public\' => true,
            \'rewrite\' => array(\'slug\' => \'portfolio\'),
            \'publicly_queryable\' => true,
            \'hierarchical\' => false,
            \'has_archive\' => true,
            \'menu_icon\' => \'dashicons-art\',
            \'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\',\'author\'),
            \'can_export\' => true,
            \'taxonomies\' => array(\'portfolio-category\'),
        )
    );
}
add_action( \'init\', \'portfolio_post_type\');

function portfolio_create_taxonomy() {
    register_taxonomy(
        \'portfolio-category\',
        \'portfolio\',
        array(
            \'label\' => __( \'Category\' ),
            \'rewrite\' => array( \'slug\' => \'portfolio-category\' ), // changed from reserved "category" slug
            \'hierarchical\' => true,
        )
    );
}
add_action( \'init\', \'portfolio_create_taxonomy\' );

结束

相关推荐

rewrite rules hierarchical

我希望我的WordPress遵循以下规则:/productes/ 包含所有产品的页面/productes/[category]/ 包含该类别所有产品的分类页面/productes/[category]/[subcategory]/ 包含该类别所有产品(与2相同)的分类页面/[productes]/[category]/[product]/ A.single-productes.php 将显示类别产品/[productes]/[category]/[subcategory]/[product]/ A.sin