X

Afficher le menu du type d’article personnalisé en tant que sous-menu

Snippets by IsItWP

Voulez-vous afficher le type d’article personnalisé comme un sous-menu dans le menu d’édition de l’article ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour afficher le menu du type d’article personnalisé en tant que sous-menu dans WordPress.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code au fichier functions.php de votre thème ou dans un plugin spécifique à votre site:

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' );

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la façon d’ajouter correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 43 meilleurs thèmes de photographie pour WordPress et comment créer un formulaire de contact sur WordPress.

Commentaires   laisser une réponse

  1. Mochammad Taufiq novembre 26, 2013 à 2:12 am

    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

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !