1

I'm trying to add custom post types entries automatically as submenu's to my main menu. 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 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 );
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.