Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP hierarichal menu of any post type. posts are auto added, like pages can be
//hijack menu and output a whole post type in hierarchical order.
//post type must have hierarchical order capability and menu name must match post type
//add post types for this inside $post_types below
add_filter( 'wp_get_nav_menu_items', 'cpt_auto_add_menu', 10, 3 );
function cpt_auto_add_menu( $items, $menu, $args ) {
$post_types = array( 'add_post_types', 'that_you_want' );
$menu_slug = $menu->slug;
if ( in_array( $menu_slug, $post_types ) ) {
$menu_order = 1;
$pages = get_pages(
array(
'post_type' => $menu_slug,
'sort_column' => 'menu_order'
)
);
$items = $pages;
//add the menu stuff
foreach ( $items as $item ) {
$item->menu_item_parent = $item->post_parent;
$item->post_type = 'nav_menu_item';
$item->object = $menu_slug;
$item->type = 'post_type';
$item->title = $item->post_title;
$item->url = get_permalink( $item->ID );
$item->menu_order = $menu_order ++;
$item->object_id = $item->ID;
$item->db_id = $item->ID;
$item->classes = array();
$item->xfn = '';
}
}
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.