如何重命名默认帖子类型的帖子

时间:2011-04-28 作者:Evie Milo

我使用posts类型的posts来显示公文包项目,而将公文包标记为posts看起来很奇怪。有没有办法将帖子重命名为公文包,以更好地反映其使用情况。

9 个回复
SO网友:elbatron

我使用以下脚本重命名默认的帖子类型:

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

    $menu[5][0] = \'Portfolio\';
    $submenu[\'edit.php\'][5][0] = \'Portfolio\';
    $submenu[\'edit.php\'][10][0] = \'New Portfolio\';
    $submenu[\'edit.php\'][16][0] = \'Portfolio Tags\';
    echo \'\';
}
add_action( \'admin_menu\', \'change_post_menu_label\' );

function change_post_object_label() {
    global $wp_post_types;

    $labels = &$wp_post_types[\'post\']->labels;
    $labels->name = \'Portfolio\';
    $labels->singular_name = \'Portfolio\';
    $labels->add_new = \'New Portfolio\';
    $labels->add_new_item = \'New Portfolio\';
    $labels->edit_item = \'Edit Portfolio\';
    $labels->new_item = \'New Portfolio\';
    $labels->view_item = \'View Portfolio\';
    $labels->search_items = \'Search Portfolio\';
    $labels->not_found = \'Not found\';
    $labels->not_found_in_trash = \'Not found in trash\';
}
add_action( \'init\', \'change_post_object_label\' );

SO网友:supajb

如果您只想重命名帖子的外观,而不是创建自定义帖子类型,请将此代码添加到主题函数中。php文件。

// hook the translation filters
add_filter(  \'gettext\',  \'change_post_to_portfolio\'  );
add_filter(  \'ngettext\',  \'change_post_to_portfolio\'  );

function change_post_to_portfolio( $translated ) {
  $translated = str_ireplace(  \'Post\',  \'Portfolio\',  $translated );  // ireplace is PHP5 only
  return $translated;
}
为了提高透明度,我从this article, 虽然我过去也用过类似的技巧。

SO网友:Chip Bennett

您需要创建一个自定义的帖子类型“公文包”。

帖子就是帖子。为什么要试着把它们当作它们不是的东西来使用,然后试着改变它们的命名法,而不是在中编写一个或两个简单的函数functions.php, 这将导致同时具有您想要的确切功能和确切命名法?

SO网友:Boldhand

// hook the translation filters
add_filter(  \'gettext\',  \'change_post_to_article\'  );
add_filter(  \'ngettext\',  \'change_post_to_article\'  );

function change_post_to_article( $translated ) {
     $translated = str_ireplace(  \'Post\',  \'Article\',  $translated );  // ireplace is PHP5 only
     return $translated;
}
我从smashing magazine得到了这个提示,并对它进行了测试,效果非常好

http://www.smashingmagazine.com/2011/05/10/new-wordpress-power-tips-for-template-developers-and-consultants/

SO网友:Nuno Sarmento

这个get_post_type_object 将完成这项工作。

add_action( \'init\', \'ns_change_post_object\' );
// Change dashboard Posts to News
function ns_change_post_object() {
   $get_post_type = get_post_type_object(\'post\');
    $labels = $get_post_type->labels;
    $labels->name = \'News\';
    $labels->singular_name = \'News\';
    $labels->add_new = \'Add News\';
    $labels->add_new_item = \'Add News\';
    $labels->edit_item = \'Edit News\';
    $labels->new_item = \'News\';
    $labels->view_item = \'View News\';
    $labels->search_items = \'Search News\';
    $labels->not_found = \'No News found\';
    $labels->not_found_in_trash = \'No News found in Trash\';
    $labels->all_items = \'All News\';
    $labels->menu_name = \'News\';
    $labels->name_admin_bar = \'News\';
}

SO网友:superhero

我在寻找将帖子类型从一个名称更改为另一个名称的解决方案时发现了这个帖子。

我没有按照这里的人的建议进行自定义查询,而是简单地执行了以下操作:

$post = get_post( $id );      // The current post id
$post->post_type = \'receipt\'; // The new post type name
wp_update_post( $post );      // Updating the new information
cpt必须ofc已经创建并格式化

SO网友:davidcondrey

将帖子重命名为公文包

function litho_posts_portfolio() {
    global $menu;
    global $submenu;
    $menu[5][0] = __("Portfolio", \'litho\');
    $submenu[\'edit.php\'][5][0] = __("Portfolio", \'litho\');
    $submenu[\'edit.php\'][10][0] = __("New Item", \'litho\');
    echo \'\';
}
function litho_posts_portfolio_label() {
    global $wp_post_types;
    $labels = &$wp_post_types[\'post\']->labels;
    $labels->name = __("Portfolio", \'litho\');
    $labels->singular_name = __("Item", \'litho\');
    $labels->add_new = __("New Item", \'litho\');
    $labels->add_new_item = __("New Item", \'litho\');
    $labels->edit_item = __("Edit Item", \'litho\');
    $labels->new_item = __("Item", \'litho\');
    $labels->view_item = __("View Item", \'litho\');
    $labels->search_items = __("Search Portfolio", \'litho\');
    $labels->not_found = __("No Item Found", \'litho\');
    $labels->not_found_in_trash = __("No Item found in Trash", \'litho\');
}
add_action( \'init\', \'litho_posts_portfolio_label\' );
add_action( \'admin_menu\', \'litho_posts_portfolio\' );

SO网友:Pippin

您只需要创建另一个具有与常规帖子相同功能的自定义帖子。然后,您可以使用以下命令禁用Posts菜单:

function remove_menus()
{
    global $menu;
    $restricted = array( __(\'Posts\'));
    end ($menu);

    while (prev($menu))
    {
        $value = explode(\' \',$menu[key($menu)][0]);

        if(in_array($value[0] != NULL?$value[0]:"" , $restricted))
        {
            unset($menu[key($menu)]);
        }
    }
}
add_action(\'admin_menu\', \'remove_menus\');

SO网友:anu

如果您只想更改Post->Portfolio中的admin菜单标签,请查看以下问题:

Changing Admin Menu Labels

[更新]

此插件Admin Menu Editor 看起来它可以让你更容易地更改菜单标签,不过我还没有测试过。

结束

相关推荐