通过子主题更改自定义帖子类型的标签

时间:2013-04-19 04:29:17

标签: wordpress wordpress-theming custom-post-type

我有一个父Wordpress主题使用自定义帖子类型的“投资组合”,但我想将其更改为“属性”。我想将所有上下文元素更改为显示“属性”,如“显示属性”,“添加新属性”,“删除属性”等。我知道我可以通过更新父主题来完成此操作,但是如果我能避免它,我宁愿不这样做。

我找到了这个帖子:https://wordpress.stackexchange.com/questions/19240/can-i-change-a-custom-post-type-label-from-a-child-theme

不幸的是,我不够精通PHP,无法编写自己的函数并改变它。有人可以帮我解决这个问题吗?我有点卡住了。

我觉得这对于php开发来说很简单。我只是不知道该怎么做。

2 个答案:

答案 0 :(得分:3)

要重命名帖子类型标签,我们需要操纵全局$wp_post_types。请注意,帖子类型为portfolio,标签只是虚拟文字。

add_action( 'init', 'object_label_so_16097216', 0 );

function object_label_so_16097216()
{
    global $wp_post_types;
    $labels = &$wp_post_types['portfolio']->labels; // <-- adjust CPT

    $labels->name = 'name'; // <-- adjust from here on
    $labels->singular_name = 'singular_name';
    $labels->add_new      = 'add_new';
    $labels->add_new_item = 'add_new';
    $labels->edit_item = 'edit_item';
    $labels->new_item = 'name';
    $labels->view_item = 'view_item';
    $labels->search_items = 'search_items';
    $labels->not_found = 'not_found';
    $labels->not_found_in_trash = 'not_found_in_trash';
}

之后,我们仍然需要更改菜单项标签。在这里,我使用辅助功能来获取CPT菜单项的确切位置。标签再次是虚拟文本。

add_action( 'admin_menu', 'menu_label_so_16097216' , 0 );

function menu_label_so_16097216()
{
    global $menu, $submenu;

    $cpt_link = 'edit.php?post_type=portfolio'; // <-- adjust CPT
    $position = b5f_recursive_array_search( $cpt_link, $menu );

    $menu[$position][0] = 'name'; // <-- adjust from here on
    $submenu[$cpt_link][5][0] = 'name';
    $submenu[$cpt_link][10][0] = 'add_new';       
}

function b5f_recursive_array_search( $needle, $haystack ) 
{
    foreach( $haystack as $key => $value ) 
    {
        $current_key = $key;
        if( 
            $needle === $value 
            OR ( 
                is_array( $value )
                && b5f_recursive_array_search( $needle, $value ) !== false 
            )
        ) 
        {
            return $current_key;
        }
    }
    return false;
}

只需将此代码放在孩子的functions.php中,调整CPT和标签即可。

基于this WordPress Answer

答案 1 :(得分:2)

在回答Jeremy Miller的回答时,您无需通过管理菜单循环更改标签。完整的标签列表是:

'name'
'singular_name'
'menu_name'
'name_admin_bar'
'all_items'
'add_new'
'add_new_item'
'edit_item'
'new_item'
'view_item'
'search_items'
'not_found'
'not_found_in_trash'
'parent_item_colon'

只需查看https://codex.wordpress.org/Function_Reference/register_post_type#Arguments

即可

由于您可以指定'menu_name',因此添加额外的钩子并循环使用Jeremy指定的菜单项是没有用的。

相关问题