更改已在父主题中注册的CPT插件和分类

时间:2016-11-30 作者:Gustavo Sartori

我在childthime中使用Read WP(premium)。这个主题在函数中声明了公文包CPT(slug:potfolio)。php。每个项目都可以嵌套在一个部门中(slug:Department)。如果我只想看到一个特定部门,URL将如下所示:

我的网站。com/部门/类别名称。

如果我想查看一个特定项目,URL将是:

我的网站。com/portfolio/my projects名称。

问题是:我怎样才能改变这两个鼻涕虫?我需要改变:

我已经在代码中找到了这个slug的注册位置,但我不知道如何重新命名它。

下面是公文包(父主题函数.php)的代码片段:

function create_post_type_portfolio()
    {
        $labels = array(\'name\' => __( \'Portfolio\', \'read\' ),
                        \'singular_name\' => __( \'Portfolio Item\', \'read\' ),
                        \'add_new\' => __( \'Add New\', \'read\' ),
                        \'add_new_item\' => __( \'Add New\', \'read\' ),
                        \'edit_item\' => __( \'Edit\', \'read\' ),
                        \'new_item\' => __( \'New\', \'read\' ),
                        \'all_items\' => __( \'All\', \'read\' ),
                        \'view_item\' => __( \'View\', \'read\' ),
                        \'search_items\' => __( \'Search\', \'read\' ),
                        \'not_found\' =>  __( \'No Items found\', \'read\' ),
                        \'not_found_in_trash\' => __( \'No Items found in Trash\', \'read\' ),
                        \'parent_item_colon\' => \'\',
                        \'menu_name\' => \'Portfolio\' );

        $args = array(  \'labels\' => $labels,
                        \'public\' => true,
                        \'exclude_from_search\' => false,
                        \'publicly_queryable\' => true,
                        \'show_ui\' => true,
                        \'query_var\' => true,
                        \'show_in_nav_menus\' => true,
                        \'capability_type\' => \'post\',
                        \'hierarchical\' => false,
                        \'menu_position\' => 5,
                        \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\', \'revisions\' ),
                        \'rewrite\' => array( \'slug\' => \'portfolio\', \'with_front\' => false ));

        register_post_type( \'portfolio\' , $args );
    }
以及部门的代码片段(父主题函数.php):

function portfolio_taxonomy()
    {
        $labels_dep = array(\'name\' => __( \'Departments\', \'read\' ),
                            \'singular_name\' => __( \'Department\', \'read\' ),
                            \'search_items\' =>  __( \'Search\', \'read\' ),
                            \'all_items\' => __( \'All\', \'read\' ),
                            \'parent_item\' => __( \'Parent Department\', \'read\' ),
                            \'parent_item_colon\' => __( \'Parent Department:\', \'read\' ),
                            \'edit_item\' => __( \'Edit\', \'read\' ),
                            \'update_item\' => __( \'Update\', \'read\' ),
                            \'add_new_item\' => __( \'Add New\', \'read\' ),
                            \'new_item_name\' => __( \'New Department Name\', \'read\' ),
                            \'menu_name\' => __( \'Departments\', \'read\' ) );

        register_taxonomy(  \'department\',
                            array( \'portfolio\' ),
                            array( \'hierarchical\' => true,
                            \'labels\' => $labels_dep,
                            \'show_ui\' => true,
                            \'public\' => true,
                            \'query_var\' => true,
                            \'rewrite\' => array( \'slug\' => \'content\' ) ) );


        $labels_tag = array(\'name\' => __( \'Portfolio Tags\', \'read\' ),
                            \'singular_name\' => __( \'Portfolio Tag\', \'read\' ),
                            \'search_items\' =>  __( \'Search\', \'read\' ),
                            \'all_items\' => __( \'All\', \'read\' ),
                            \'parent_item\' => __( \'Parent Portfolio Tag\', \'read\' ),
                            \'parent_item_colon\' => __( \'Parent Portfolio Tag:\', \'read\' ),
                            \'edit_item\' => __( \'Edit\', \'read\' ),
                            \'update_item\' => __( \'Update\', \'read\' ),
                            \'add_new_item\' => __( \'Add New\', \'read\' ),
                            \'new_item_name\' => __( \'New Portfolio Tag Name\', \'read\' ),
                            \'menu_name\' => __( \'Portfolio Tags\', \'read\' ) );

        register_taxonomy(  \'portfolio_tags\',
                            array( \'portfolio\' ),
                            array( \'hierarchical\' => false,
                            \'labels\' => $labels_tag,
                            \'show_ui\' => true,
                            \'public\' => true,
                            \'query_var\' => true,
                            \'rewrite\' => array( \'slug\' => \'portfolio_tags\' ) ) );
    }
    // end portfolio_taxonomy
那么,在不编辑父主题函数的情况下,如何超越这段代码呢?

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

WordPress 4.5介绍了功能unregister_post_type()unregister_taxonomy(). 您可以分别取消注册帖子类型分类法,并使用自定义设置再次注册它们。帖子类型仍命名为portfolio. 您可以重命名它,但可能需要检查模板文件中的引用。

add_action(\'init\', \'wpse_247924_overwrite_theme_post_types\', 1000);
function wpse_247924_overwrite_theme_post_types() {

    unregister_post_type( \'portfolio\' );

    $labels = array(
        \'name\' => __( \'Portfolio\', \'read\' ),
        \'singular_name\' => __( \'Portfolio Item\', \'read\' ),
        \'add_new\' => __( \'Add New\', \'read\' ),
        \'add_new_item\' => __( \'Add New\', \'read\' ),
        \'edit_item\' => __( \'Edit\', \'read\' ),
        \'new_item\' => __( \'New\', \'read\' ),
        \'all_items\' => __( \'All\', \'read\' ),
        \'view_item\' => __( \'View\', \'read\' ),
        \'search_items\' => __( \'Search\', \'read\' ),
        \'not_found\' =>  __( \'No Items found\', \'read\' ),
        \'not_found_in_trash\' => __( \'No Items found in Trash\', \'read\' ),
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Portfolio\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'show_in_nav_menus\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => 5,
        \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\', \'revisions\' ),
        \'rewrite\' => array(
            \'slug\' => \'conteudo\',
            \'with_front\' => false
        )
    );

    register_post_type( \'portfolio\' , $args );

    unregister_taxonomy( \'department\' );

    $labels_dep = array(
        \'name\' => __( \'Departments\', \'read\' ),
        \'singular_name\' => __( \'Department\', \'read\' ),
        \'search_items\' =>  __( \'Search\', \'read\' ),
        \'all_items\' => __( \'All\', \'read\' ),
        \'parent_item\' => __( \'Parent Department\', \'read\' ),
        \'parent_item_colon\' => __( \'Parent Department:\', \'read\' ),
        \'edit_item\' => __( \'Edit\', \'read\' ),
        \'update_item\' => __( \'Update\', \'read\' ),
        \'add_new_item\' => __( \'Add New\', \'read\' ),
        \'new_item_name\' => __( \'New Department Name\', \'read\' ),
        \'menu_name\' => __( \'Departments\', \'read\' )
    );

    register_taxonomy(
        \'material\',
        array( \'portfolio\' ),
        array(
            \'hierarchical\' => true,
            \'labels\' => $labels_dep,
            \'show_ui\' => true,
            \'public\' => true,
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'material\'
            )
        )
    );


    $labels_tag = array(
        \'name\' => __( \'Portfolio Tags\', \'read\' ),
        \'singular_name\' => __( \'Portfolio Tag\', \'read\' ),
        \'search_items\' =>  __( \'Search\', \'read\' ),
        \'all_items\' => __( \'All\', \'read\' ),
        \'parent_item\' => __( \'Parent Portfolio Tag\', \'read\' ),
        \'parent_item_colon\' => __( \'Parent Portfolio Tag:\', \'read\' ),
        \'edit_item\' => __( \'Edit\', \'read\' ),
        \'update_item\' => __( \'Update\', \'read\' ),
        \'add_new_item\' => __( \'Add New\', \'read\' ),
        \'new_item_name\' => __( \'New Portfolio Tag Name\', \'read\' ),
        \'menu_name\' => __( \'Portfolio Tags\', \'read\' )
    );

    register_taxonomy(
        \'portfolio_tags\',
        array( \'portfolio\' ),
        array(
            \'hierarchical\' => false,
            \'labels\' => $labels_tag,
            \'show_ui\' => true,
            \'public\' => true,
            \'query_var\' => true,
            \'rewrite\' => array(
                \'slug\' => \'portfolio_tags\'
            )
        )
    );

}

相关推荐

Slug for custom post type

我在我的网站上使用网页和博客帖子。页面获取URL示例。org/%postname%/,并基于Permalink设置,posts获取URL示例。组织/博客/%postname%/。完美的我有一个自定义的帖子类型,由我网站上的另一个组用于他们的网页。在注册post类型时,我为它们提供了一个重写slug:\'rewrite\' => array(\'slug\' => \'ncfpw\'),然而,他们的页面得到了URL示例。组织/博客/ncfpw/%博文名%/我怎样才能摆脱;博客;在他们的URL中?