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

时间:2013-04-19 作者:Jeremy Miller

我有一个WordPress父主题,它使用一个名为portfolio, 我想换成property.

我想将所有上下文元素都更改为显示property 喜欢"show properties", "add new property", "delete property", 等等。我知道我可以通过更新父主题来做到这一点,但如果可以避免的话,我宁愿不这样做。

我发现this answer, 但不幸的是,我对PHP不够了解,无法编写自己的函数并进行更改。

有人能帮我解决这个问题吗?我有点困了
我觉得这对于PHP开发人员来说很简单。我只是不知道怎么做。

2 个回复
最合适的回答,由SO网友:Vinod Dalvi 整理而成

$wp_post_types 是一个可容纳post_type 对象,这些对象依次具有labels 属性
您可以更改$wp_post_types[$post_type]->labels 在父主题设置了CPT之后。

将更高的优先级添加到init

在主题中添加以下代码functions.php 文件

有关更多信息,请查看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 );
在中添加以下代码functions.php 从侧栏替换主菜单标签

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\' );
在中添加以下代码functions.php 用于将图像添加到管理侧栏CPT菜单

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
}

SO网友:Christina

这将更改标签和菜单名称。还有一个额外的好处,就是更换弹头。

此示例将名为“服务”的注册帖子类型更改为“专业”(美式英语拼写)。

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.com/change-the-wordpress-post-type-name-to-something-else/
 * https://revelationconcept.com/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;
}

结束

相关推荐