admin管理员组

文章数量:1391947

I have a WordPress parent theme that uses a custom post type called portfolio, which I'd like to change to property.

I'd like to have all of the contextual elements changed to show property like, "show properties", "add new property", "delete property", etc.
I know I can do this by updating the parent theme, but I'd prefer not to, if I can avoid it.

I found this answer, but unfortunately, I'm not PHP savvy enough to write my own function and change this.

Can someone help me out with this? I'm kind of stuck.
I have a feeling this is something simple for a PHP Dev. I just don't know how to do it.

I have a WordPress parent theme that uses a custom post type called portfolio, which I'd like to change to property.

I'd like to have all of the contextual elements changed to show property like, "show properties", "add new property", "delete property", etc.
I know I can do this by updating the parent theme, but I'd prefer not to, if I can avoid it.

I found this answer, but unfortunately, I'm not PHP savvy enough to write my own function and change this.

Can someone help me out with this? I'm kind of stuck.
I have a feeling this is something simple for a PHP Dev. I just don't know how to do it.

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Apr 19, 2013 at 4:15 Jeremy MillerJeremy Miller 1052 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

$wp_post_types is a global array that holds post_type objects, which in turn have a labels property.
You can change $wp_post_types[$post_type]->labels after the parent theme has set the CPT.

Add higher priority to init hook.

Add the following code in your theme's functions.php file.

For more information, check out the codex article on get_post_type_object.

function change_post_object_label() {
    global $wp_post_types;
    $labels = &$wp_post_types['portfolio']->labels;
    $labels->name = 'Property';
    $labels->singular_name = 'Property';
    $labels->add_new = 'Add Property';
    $labels->add_new_item = 'Add Property';
    $labels->edit_item = 'Edit Property';
    $labels->new_item = 'Property';
    $labels->all_items = 'All Properties';
    $labels->view_item = 'View Property';
    $labels->search_items = 'Search Property';
    $labels->not_found = 'No Property found';
    $labels->not_found_in_trash = 'No Property found in Trash';    
}
add_action( 'init', 'change_post_object_label', 999 );

Add following code in functions.php to replace main menu label from sidebar

function change_post_menu_label() {
  global $menu;
  //print_r($menu); Print menus and find out the index of your custom post type menu from it.
  $menu[27][0] = 'Bacons'; // Replace the 27 with your custom post type menu index from displayed above $menu array 
}
add_action( 'admin_menu', 'change_post_menu_label' );

Add following code in functions.php for adding image to admin sidebar CPT menu

add_action('admin_head', 'change_CPT_icon');
function change_CPT_icon() {?>
  <style>
    #menu-posts-portfolio .wp-menu-image { background: url('<?php echo  get_bloginfo('template_directory').'/includes/images/new-image.png';?>') no-repeat 5px 5px transparent !important; }     
  </style>
  <?php
}

This changes the labels and the menu name. Also a bonus to change the slug too.

This example changes a registered post type called 'services' to 'specialty' (American English spelling).

Change the labels and the name all at once:

add_action( 'init', 'yourprefix_change_cpt_service_to_specialty_labels' );
/**
 * Change Post Type Post Labels to Blog
 * Reference(s):
 * https://wpbeaches/change-the-wordpress-post-type-name-to-something-else/
 * https://revelationconcept/wordpress-rename-default-posts-news-something-else/
 */
function yourprefix_change_cpt_service_to_specialty_labels() {

    if ( post_type_exists( 'services' ) ) :

        $get_post_type = get_post_type_object('services');
        $labels = $get_post_type->labels;
        $labels->name               = 'Specialty';
        $labels->singular_name      = 'Specialty Page';
        $labels->add_new            = 'Add Specialty Page';
        $labels->add_new_item       = 'Add Specialty Page';
        $labels->edit_item          = 'Edit Specialty Page';
        $labels->new_item           = 'New Specialty Page';
        $labels->view_item          = 'View Specialty Page';
        $labels->search_items       = 'Search Specialty Pages';
        $labels->not_found          = 'No Specialties found';
        $labels->not_found_in_trash = 'No Specialty Pages found in Trash';
        $labels->all_items          = 'All Specialty Pages';
        $labels->menu_name          = 'Specialties';
        $labels->name_admin_bar     = 'Specialty Page';

    endif;
}

Bonus: Change the slug. Don't forget to save your permalinks after this is added.

add_filter( 'register_post_type_args', 'yourprefix_change_post_types_slug_service', 10, 2 );
// Change Slug of Registered Post Type for Services // Resave your PERMALINKS or this will return 404!!!
function yourprefix_change_post_types_slug_service( $args, $post_type ) {

   /*item post type slug*/   
   if ( 'services' === $post_type ) :
      $args['rewrite']['slug'] = 'specialty';
   endif;

   return $args;
}

本文标签: Change labels of custom post type via child theme