Skip to content

Instantly share code, notes, and snippets.

@saltnpixels
Created July 25, 2018 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saltnpixels/8625ee62fab4e8fd5c176d3ba2de7125 to your computer and use it in GitHub Desktop.
Save saltnpixels/8625ee62fab4e8fd5c176d3ba2de7125 to your computer and use it in GitHub Desktop.
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