X

Mostrar menú personalizado como submenú

Snippets by IsItWP

¿Quieres mostrar el tipo de entrada personalizada como un submenú dentro del menú de edición de entradas? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que se puede utilizar para mostrar el menú de tipo de entrada personalizada como submenú en WordPress.

Instrucciones:

Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:

function nacin_register_slideshows_post_type() {
	register_post_type( 'slideshow', array(
		'labels' => array(
			'name' => 'Slideshows',
			'singular_name' => 'Slideshow',
		),
		'public' => true,
		'show_ui' => true,
		'show_in_menu' => 'edit.php',
		'supports' => array( 'title' ,'thumbnail', 'editor' ),
	) );
}
add_action( 'init', 'nacin_register_slideshows_post_type' );

Nota: Si es la primera vez que añades fragmentos de código en WordPress, consulta nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente tu sitio.

Si te ha gustado este fragmento de código, por favor, considere revisar nuestros otros artículos en el sitio como: 43 mejores temas de fotografía para WordPress y cómo crear un formulario de contacto en WordPress.

Comentarios   Deja una respuesta

  1. i register post_type with add new item, but ‘add new item’ not show in submenu, any suggestion for my problem ?

  2. Nice. Trying to take this a step further and attach the post type to a custom menu page. So far no cigar… any thoughts?

    1. Your question is old but figured this might come in handy for someone else.
      I just spent a long time looking through the WP source code trying to do the
      same thing: I have a custom menu created using add_menu_page(). It has a handful
      of sub_menu items. I have a custom post type that is related to this menu and I
      wanted the add / list links for that post type to be submenu items.

      Short answer: No easy way to do it in WP. The way it
      generates the menus and custom post type UI means there is no simple call you
      can make.

      Solution:
      1. Register your post type with the
      following options:
      ‘show_ui’ => true,
      ‘show_in_menu’ => false
      In
      this example my custom post type is acct_notes

      2. In your theme’s functions.php file or your plugin file, add an action to
      admin_menu:
      add_action(‘admin_menu’,
      ‘modify_admin_menus’);

      3. Add submenu_pages for the two post UI links — list posts, create new
      post.
      The first param is the slug of the menu you’re adding it to. The second
      is the name that will show up on the page. The third is what the link will show
      up as. The fourth is the capability required to access it. The fifth is the slug
      for this menu item. And last is the function that will generate the output. In
      this case we’re not going to use it so we can pass NULL.

      add_submenu_page(‘members’, ‘Account Notes’, ‘Account Notes’,
      ‘manage-options’, ‘view_account_notes’, NULL);

      4. The last step is the function that will change those submenu links so
      that they go to the correct post UI pages.

      function modify_admin_menus(){
      global $submenu;

      if(array_key_exists(‘members’, $submenu)){

      foreach($submenu[‘members’] as $key => $value){
      $k =
      array_search(‘view_account_notes’, $value);
      if($k){

      $submenu[‘members’][$key][$k] =
      (current_user_can($submenu[‘members’][$key][1]))?
      admin_url(‘/edit.php?post_type=acct_notes’):”;
      }

      $l = array_search(‘new_account_note’, $value);

      if($l){
      $submenu[‘members’][$key][$l] =
      (current_user_can($submenu[‘members’][$key][1]))?
      admin_url(‘/post-new.php?post_type=dojo_acct_notes’) : ”;}

      }
      }
      }

      What’s going on in the function is that it’s checking the global $submenu
      array to see if our top level menu item (members) exists — it might not if the
      user doesn’t have permission to access it. If they do it then looks to see if
      that top level menu item has a submenu page with the slug “view_account_notes”
      — what we assigned in add submenu_page. Finally, it checks the permission we
      assigned in add_submenu_page and if the current user has that permission it
      outputs the new link. If they don’t it removes the link altogether.

      We do that same check twice — once for the link to list our custom post
      type, and once for the link to add a new custom post type.

      It would be far preferable to have this capability built in, but this will
      let you add custom post type user interfaces to any menu you want.

  3. Thank you very much. Can you help me, please? I am having problem: I want create a menu in WordPress admin contained many child custom post type to easy to manage. Example:

    Menu Facilities include post types: – Meetings; – Weddings; – SPA

    Thanks for help very much.

  4. good hack.thanks.

    1. no problem anytime,

    2.  It’s not a hack, it’s a feature

Añadir un comentario

Nos alegra que haya decidido dejar un comentario. Tenga en cuenta que todos los comentarios se moderan de acuerdo con nuestra política de privacidad , y que todos los enlaces son nofollow. NO utilice palabras clave en el campo del nombre. Tengamos una conversación personal y significativa.

WordPress Launch Checklist

La lista definitiva para lanzar WordPress

Hemos recopilado todos los elementos esenciales de la lista de comprobación para el lanzamiento de su próximo sitio web de WordPress en un práctico ebook.
Sí, envíeme el ¡gratuito!