sub menu – Add custom posts to submenu automatically
Question
I’m trying to automatically add posts from a CPT to a submenu in Appearance > Menus.
I’ve tried following this code suggested by @Bauwb
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 );
}
Auto add (multiple) Custom Post Types to menu
but I can’t get it to work.
Do I need to add the class nestjes-parent-item somewhere other than the custom CSS field on the menu item?
Thanks
0
7 months
2022-06-30T07:28:07-05:00
2022-06-30T07:28:07-05:00 0 Answers
0 views
0
Leave an answer