如何重命名已经注册的类别?

时间:2018-12-07 07:18:13

标签: php wordpress categories

如何将默认类别名称重命名为自定义名称?

我想将默认类别名称重命名为我的自定义名称,我已经使用了以下代码,但没有用。

function wpse_modify_taxonomy() {
    // get the arguments of the already-registered taxonomy
    $people_category_args = get_taxonomy( 'category' ); // returns an object

    // make changes to the args
    // in this example there are three changes
    // again, note that it's an object
    $people_category_args->show_admin_column = true;
    $people_category_args->rewrite['slug'] = 'post';
    $people_category_args->rewrite['with_front'] = false;

    // re-register the taxonomy
    register_taxonomy( 'category', 'post', (array) $people_category_args );
}
// hook it up to 11 so that it overrides the original register_taxonomy function
add_action( 'init', 'wpse_modify_taxonomy', 11 );

有人知道怎么可能吗?

1 个答案:

答案 0 :(得分:0)

是的,您可以将默认类别名称更改为您想要的其他名称。

首先,让我们在WordPress管理员的菜单项中更改默认标签。您可以将此代码复制到functions.php文件

function revcon_change_cat_label() {
    global $submenu;
    $submenu['edit.php'][15][0] = 'MyCategories'; // Rename categories to MyCategories
}
add_action( 'admin_menu', 'revcon_change_cat_label' );

这将更改菜单项中的类别名称标签。

现在,让我们在整个管理员中更新其他标签(元框等),您可以将此代码直接粘贴在用于重命名菜单标签的代码下方。

function revcon_change_cat_object() {
    global $wp_taxonomies;
    $labels = &$wp_taxonomies['category']->labels;
    $labels->name = 'MyCategories';
    $labels->singular_name = 'MyCategories';
    $labels->add_new = 'Add MyCategories';
    $labels->add_new_item = 'Add MyCategories';
    $labels->edit_item = 'Edit MyCategories';
    $labels->new_item = 'MyCategories';
    $labels->view_item = 'View MyCategories';
    $labels->search_items = 'Search MyCategories';
    $labels->not_found = 'No MyCategories found';
    $labels->not_found_in_trash = 'No MyCategories found in Trash';
    $labels->all_items = 'All MyCategories';
    $labels->menu_name = 'MyCategories';
    $labels->name_admin_bar = 'MyCategories';
}
add_action( 'init', 'revcon_change_cat_object' );

这样,您可以将默认类别名称重命名为自定义名称。

尝试一下,让我知道是否有任何问题。