在 WordPress 管理员中重命名分类标签

问题描述 投票:0回答:1

我正在尝试重命名已购买主题的分类法。我已成功地将 slug 从“Director”重命名为“Instructor”,并在我的functions.php 文件中使用以下代码。

function overwrite_director_slug( $taxonomy, $object_type, $args ){
    if( 'video-director' == $taxonomy ){
        remove_action( current_action(), __FUNCTION__ );
        $args['rewrite'] = array('slug' => 'instructor');
        register_taxonomy( $taxonomy, $object_type, $args );
    }
}
add_action( 'registered_taxonomy', 'overwrite_director_slug', 10, 3 );

如何重命名管理菜单中的标签?我想再次将名字从“主任”改为“讲师”。 (见图)

wordpress wordpress-theming custom-post-type custom-taxonomy
1个回答
0
投票

在现有分类法上重命名您想要的所有内容非常容易。

您可以首先使用 get_taxonomy() WordPress 函数可视化您要重命名的内容。以下内容显示在前端页脚中,仅适用于管理员用户角色,您的分类的所有数据:

add_action('wp_footer', 'wp_footer_script_test_output', 10);
function wp_footer_script_test_output() {
    if( ! current_user_can('administrator') ) return;

    echo '<pre>'. print_r( get_taxonomy('video-director'), true ) . '</pre>';
}

现在您可以看到所有可以重命名的数据,并且可以删除该代码。

以下内容将根据您的代码重命名管理菜单中的分类标签(以及可选的其他一些内容)

function overwrite_director_slug( $taxonomy, $object_type, $args ){
    if( 'video-director' == $taxonomy ){
        remove_action( current_action(), __FUNCTION__ );
        $args['rewrite'] = array('slug' => 'instructor'); 

        $labels = $args['labels']; // As it's an object we need to set it in a variable

        $labels->menu_name = 'Instructor'; // <=== HERE For the "MENU NAME"

        $labels->singular_name = 'Instructor'; // (optional)
        $labels->name = 'Instructor'; // (optional)

        $args['labels'] = $labels; // Set back the modified Object

        $args['label'] = 'Instructor';  // General label (optional)

        register_taxonomy( $taxonomy, $object_type, $args );
    }
}
add_action( 'registered_taxonomy', 'overwrite_director_slug', 10, 3 );

代码位于子主题的functions.php 文件中(或插件中)。已测试并工作。

© www.soinside.com 2019 - 2024. All rights reserved.