【问题标题】:adding a custom post type as a sub-menu of another existing page menu添加自定义帖子类型作为另一个现有页面菜单的子菜单
【发布时间】:2015-12-20 08:45:47
【问题描述】:

我通过此代码在管理面板中添加了一个页面菜单,它工作正常。

add_menu_page('Ads Dashboard', 'Ads Dashboard', 'administrator', 'adverts', 'advert_admin_options');

现在我需要在这个菜单下添加/注册帖子类型,但它不能正常工作。我的代码似乎有问题,这是我的全部代码:

function add_ads_post_type(){ 
    register_post_type('ads', array(
        'labels' => array(
            'name' => __('Adverts', 'theme'),
            'singular_name' => __('Adverts', 'theme'),
            'menu_name' => __('Adverts', 'theme'),
            'add_new' => __('Add Advert Item', 'theme'),
            'add_new_item' => __('Add New Advert item', 'theme'),
            'edit_item' => __('Edit Advert item', 'theme'),
            'new_item' => __('New Advert item', 'theme'),
            'view_item' => __('View Advert item', 'theme'),
            'search_items' => __('Search Advert items', 'theme'),
            'not_found' => __('No Advert found', 'theme'),
            'not_found_in_trash' => __('No Advert items found in Trash', 'theme'),
        ),
    'public' => TRUE,
        'rewrite' => array('slug' => 'ads', 'with_front' => false),
        'has_archive' => true,
        'supports' => array('title', 'editor'),
        'show_in_menu' => 'admin.php?page=adverts'
    )); 
}
function advert_add_to_menu() {
    if (is_admin()) {
        add_menu_page('Ads Dashboard', 'Ads Dashboard', 'administrator', 'adverts', 'advert_admin_options');
        add_submenu_page('adverts', 'Ads', 'Ads', 10, 'ads-list', 'add_ads_post_type' );
    }
}
add_action('admin_menu', 'advert_add_to_menu');

【问题讨论】:

    标签: wordpress ads


    【解决方案1】:

    为什么这么难的家伙? 只需使用show_in_menu arg。比如:'show_in_menu' => adverts'adverts 是菜单uid 的自定义菜单元素的情况下。

    如果你想放入已经存在的菜单元素,比如在另一个帖子类型的菜单块下,那么你可以使用'show_in_menu' => 'edit.php?post_type={CTP}'

    https://imtiazrayhan.com/multiple-custom-post-types-menu-section/

    我知道它很旧,但也许这样可以节省一些时间:)

    【讨论】:

      【解决方案2】:

      试试这个:帖子类型是“广告”

      //REMOVE MENU
      
      
      function remove_menu_CPT_ads() {  
              remove_menu_page( 'edit.php?post_type=ads' );  
      }
      add_action( 'admin_menu', 'remove_menu_CPT_ads' );
      function add_my_menu(){
       add_submenu_page('adverts', 'Ads', 'Ads', 'manage_options', 'my-top-level-slug','Your_function');
      }
       add_action('admin_menu','add_my_menu');
       function Your_function(){   
           wp_redirect(admin_url().'/edit.php?post_type=ads');
           exit;
       }
       function add_ob_start(){
          if(is_admin()){
           ob_start();
          }
       }
       add_action('admin_init', 'add_ob_start');
      

      【讨论】:

      • 不通过此代码,它在一般页面中显示“自定义帖子类型菜单中的自定义页面”,但我想将自定义帖子类型添加为子菜单@vrajesh
      • 现在我像这样更改了我的代码,'show_in_menu' => 'adverts' 和 add_submenu_page('adverts', 'Ads', 'Ads', 'manage_options', 'adverts-list', 'add_ads_post_type' );它在主页下添加了广告子菜单,但不创建帖子类型只是页面,你怎么看? @vrajesh
      • 我只有一种帖子类型(广告)。请参阅我有一个页面菜单 (admin.php?page=adverts) 并想在其下添加帖子类型。 @vrajesh
      【解决方案3】:
       <?php
          add_menu_page('Page title', 'Top-level menu title', 'manage_options', 'my-top-level-handle', 'my_magic_function');
          add_submenu_page( 'my-top-level-handle', 'Page title', 'Sub-menu title', 'manage_options', 'my-submenu-handle', 'my_magic_function');
          ?>
      

      这是在自定义帖子类型菜单块下添加选项页面的示例(另请参见此处):

      <?php 
      add_submenu_page('edit.php?post_type=wiki', 'Options', 'Options', 'manage_options', 'wiki-options', array(&$this, 'options_page') );
       ?>
      

      更多信息请看这里:https://codex.wordpress.org/Administration_Menus

      【讨论】:

        【解决方案4】:

        我知道这是一个老问题,经过批准的答案确实有效,但我发现以下博客文章非常有帮助。 https://shellcreeper.com/how-to-add-wordpress-cpt-admin-menu-as-sub-menu/

        为了快速开始,请使用以下内容。阅读完整的博客以使突出显示/活动页面正常工作:

        1. show_in_menu 参数必须为 false

          register_post_type( 'your-cpt', array(
          'description'           => '',
          'public'                => true,
          'show_ui'               => true,
          'show_in_menu'          => false, /* Do not show admin menu */
          [...] 
          ) );
          
        2. 向父级添加子菜单

          add_submenu_page(
              'Ads_Dashboard',              // parent slug
              'Your CPT Title',             // page title
              'Sub Menu Title',             // sub-menu title
              'edit_posts',                 // capability
              'edit.php?post_type=your-cpt' // your menu menu slug
          );
          

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-30
          • 1970-01-01
          • 1970-01-01
          • 2015-07-24
          相关资源
          最近更新 更多