首页 > 解决方案 > 自定义帖子类型未显示在管理栏中

问题描述

我在 mu_plugins 文件夹中创建了自定义帖子类型。但它没有显示在管理菜单栏中。我尝试将代码粘贴到functions.php 中,但没有任何变化。我使用了 register_post_type 函数来创建新的帖子类型。请参阅下面的代码

 <?php
function odays_post_types()
{
  register_post_type('hotels', array(
    'capability_type' => 'hotels',
    'map_meta_cap' => true,
    'rewrite' => array('slug' => 'hotels'),
    'show_in_rest' => true,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields'),
    'has_archive' => true,
    'public' => false,
    'show_ui' => true,
    'labels' => array(
      'name' => 'Hotels',
      'add_new_item' => 'Add New Hotel',
      'edit_item' => 'Edit Hotel',
      'all_items' => 'All Hotels',
      'singular_name' => 'Hotel'
    ),
    'menu_icon' => 'dashicons-admin-multisite'
  ));
  register_post_type('clinic', array(
    'capability_type' => 'clinic',
    'map_meta_cap' => true,
    'rewrite' => array('slug' => 'clinic'),
    'show_in_rest' => true,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields'),
    'public' => true,
    'labels' => array(
      'name' => 'Clinics',
      'add_new_item' => 'Create New Clinic',
      'edit_item' => 'Edit Clinic',
      'all_items' => 'All Clinics',
      'singular_name' => 'Clinic'
    ),
    'menu_icon' => 'dashicons-plus-alt'
  ));
  register_post_type('agent', array(
    'capability_type' => 'agent',
    'map_meta_cap' => true,
    'rewrite' => array('slug' => 'agent'),
    'show_in_rest' => true,
    'public' => true,
    'labels' => array(
      'name' => 'Agents',
      'add_new_item' => 'Create New Agent',
      'edit_item' => 'Edit Agent',
      'all_items' => 'All Agents',
      'singular_name' => 'Agent'
    ),
    'menu_icon' => 'dashicons-plus-alt'
  ));
}
add_action('init', 'odays_post_types');
 ?>

请帮忙

标签: phpwordpress

解决方案


有一种更好更简单的方法来创建自定义帖子类型,您可以使用插件https://wordpress.org/plugins/custom-post-type-ui/并在创建帖子类型之后

在创建自定义帖子类型后,您可以根据需要删除插件,并且您可以获得这样的代码,只需复制此代码并在 function.php 中过去,它也可以在没有插件的情况下正常工作。在此处输入图像描述


推荐阅读