Support » Developing with WordPress » Auto add (different) Custom Post Types as submenu to parent menu item

  • Resolved bauwb

    (@bauwb)


    I’m trying to add the posts from (different) Custom Post Types automatically as submenus to different parent menu items. I’ve managed to add one CPT “nestjes” by using the code below.

    Now I would like to add two more CPT’s automatically to my menu to different parent items on the same website: “poezen” and “katers”. How do I blend in these variables in the code below?

    • my_theme_poezen_menu_filter

      poezen-parent-item

      poezen-post-type

    • my_theme_katers_menu_filter

      katers-parent-item

      katers-post-type

    • Thank you for the help.

      add_filter( 'wp_get_nav_menu_items', 'my_theme_nestjes_menu_filter',10, 3 );
      
      function my_theme_nestjes_menu_filter( $items, $menu, $args ) {
        $child_items = array(); 
        $menu_order = count($items); 
        $parent_item_id = 0; 
      
        
        foreach ( $items as $item ) {
          if ( in_array('nestjes-parent-item', $item->classes) ){
              $parent_item_id = $item->ID;
          }
        }
      
        if($parent_item_id > 0){
      
            foreach ( get_posts( 'post_type=nestjes-post-type&numberposts=-1' ) as $post ) {
              $post->menu_item_parent = $parent_item_id;
              $post->post_type = 'nav_menu_item';
              $post->object = 'custom';
              $post->type = 'custom';
              $post->menu_order = ++$menu_order;
              $post->title = $post->post_title;
              $post->url = get_permalink( $post->ID );
              array_push($child_items, $post);
            }
      
        }
      
        return array_merge( $items, $child_items );
      }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The easiest way would be to copy the same code within the callback function two more times, then alter each copy’s key parameters as needed.

    To compact the code to eliminate redundancy, place the varying parameters into an array structure, then foreach through the array to execute the same code multiple times, using the different array values where necessary.

    Thread Starter bauwb

    (@bauwb)

    I appreciate your reply very much but unfortunately I have to admit I’m still a newbie in code. I’ve tried a lot of stuff already but everything I tried gave me errors.

    How would the correct code look like? Could you help me with that? Highly appreciated!

    Thread Starter bauwb

    (@bauwb)

    A friend of mine helped me out. This is how I fixed it:

    add_filter('wp_get_nav_menu_items', 'my_theme_nestjes_menu_filter', 10, 3);
    
    function my_theme_nestjes_menu_filter($items, $menu, $args) {
      $menu_order = count($items);
    
      $mapping = [
        // class => post_type
        'nestjes-parent-item' => 'nestjes-post-type',
        'katers-parent-item' => 'katers-post-type',
        'poezen-parent-item' => 'poezen-post-type',
      ];
    
      foreach ($mapping as $class => $post_type) {
        // Find menu items with class.
        $items_with_class = array_filter($items, function($item) use ($class) {
    
          return is_array($item->classes) ? in_array($class, $item->classes) : FALSE;
        });
    
        // We found items.
        if (!empty($items_with_class)) {
          // Use first menu item.
          $first_item_with_class = reset($items_with_class);
    
          // Load all posts within the post_type.
          foreach (get_posts('post_type=' . $post_type . '&numberposts=-1') as $post) {
            // Build new menu items.
            $post->menu_item_parent = $first_item_with_class->ID;
            $post->post_type = 'nav_menu_item';
            $post->object = 'custom';
            $post->type = 'custom';
            $post->menu_order = ++$menu_order;
            $post->title = $post->post_title;
            $post->url = get_permalink($post->ID);
    
            // Add item to menu.
            array_push($items, $post);
          }
        }
      }
    
      return $items;
    }
    • This reply was modified 3 years, 2 months ago by bauwb.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Auto add (different) Custom Post Types as submenu to parent menu item’ is closed to new replies.