Created
January 17, 2018 15:20
-
-
Save solid-pixel/67c03fdbb6c99522da61dab9e722a291 to your computer and use it in GitHub Desktop.
Add custom post types to submenu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //remove this line | |
add_filter( 'wp_get_nav_menu_items', 'cpt_locations_filter', 10, 3 ); | |
function cpt_locations_filter( $items, $menu, $args ) { | |
$child_items = array(); | |
$menu_order = count($items); | |
$parent_item_id = 0; | |
foreach ( $items as $item ) { | |
if ( in_array('locations-menu', $item->classes) ){ //add this class to your menu item | |
$parent_item_id = $item->ID; | |
} | |
} | |
if($parent_item_id > 0){ | |
foreach ( get_posts( 'post_type=cpt-post-type-here&numberposts=-1' ) as $post ) { //edit cpt slugh here | |
$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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment