最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to rename default posts-type Posts

programmeradmin1浏览0评论

I'm using the posts-type Posts to display portfolio items and it looks strange to have portfolio labeled as posts. Is there any way to rename Posts to Portfolio instead to better reflect it's usage.

I'm using the posts-type Posts to display portfolio items and it looks strange to have portfolio labeled as posts. Is there any way to rename Posts to Portfolio instead to better reflect it's usage.

Share Improve this question edited Jun 19, 2014 at 5:22 davidcondrey 4997 silver badges26 bronze badges asked Apr 28, 2011 at 13:49 Evie MiloEvie Milo 1011 gold badge1 silver badge3 bronze badges 1
  • this plugin changes post types: wordpress/extend/plugins/post-type-switcher i think this one works more in bulk wordpress/extend/plugins/convert-post-types – helgatheviking Commented Jul 29, 2011 at 4:22
Add a comment  | 

9 Answers 9

Reset to default 12

I used the following script to rename the default post type:

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' );

If you want to simply rename the appearance of posts, rather than creating a custom post type then add this code to your themes functions.php file.

// 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;
}

In the interests of transparency I got this code from this article, although I have used similar tricks in the past.

You need to create a Custom Post Type, "Portfolio".

Posts are Posts. Why try to use them as something they're not, and then try to change their nomenclature, instead of writing one or two simple functions in functions.php, that will result in having both the exact functionality and the exact nomenclature that you want?

The get_post_type_object will do the work.

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';
}
// 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;
}

I got this tip from smashing magazine and tested it and it works great

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

I found this thread when I was looking for a solution to change the post type from one name to an other.

Instead of doing a custom query as suggested by someone in here I simply did this:

$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

The cpt have to ofc already have been created and formated..

Rename posts to portfolio

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' );

If you just want to change the admin menu label from Post -> Portfolio, then look at this question:

Changing Admin Menu Labels

[Update]

This plugin Admin Menu Editor looks like it will allow you to change menu labels more easily - I haven't tested it though.

You will just need to create another custom post with the same capabilities as a regular post. You can then disable the Posts menu with this:

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');
发布评论

评论列表(0)

  1. 暂无评论