I have created a Custom Post Type called “Collections”.
I’d like to have in the Nav Menu (no custom menus involved) a menu item ‘Collections’ that links to the index
page of Collections Custom Post Type, and hanging from this Menu Item, a list of n
Submenu Items each linking to a single post
.
I’ve done some research and I think there is no literature about the subject. Probably some hooking involved (rewriting the wp_nav_menu()
perhaps).
I’ve managed to have the ‘Collections’ menu item by declaring 'show_in_nav_menus' => true
in register_post_type()
but I don’t know how to move forward in making the single posts appear as a submenu.
Any help?
Note: using a Thematic Child Theme
Here’s an example on modifying a menu element to add things. In this example, a category item is extended to have a dynamically generated sub menu listing posts, where the menu item has a class of showposts-X where X is the number of posts to show:
function menu_show_category_posts( $item_output="", $item = '', $depth="", $args="" ) {
global $post;
$item_output = icit_add_class_attrib( $item_output, 'depth-' . ( $depth ? $depth : 1 ) );
if ( $item->type == 'taxonomy' && $item->object == 'category' && is_array( $item->classes ) ) {
$query = false;
$showposts = 10;
$preview = 0;
$new_html = array( );
$i = 0;
foreach( $item->classes as $class ) {
if ( preg_match( '/^showposts-?(\d*)/', $class, $matches ) ) {
$query = true;
$showposts = ! empty( $matches[ 1 ] ) && intval( $matches[ 1 ] ) ? intval( $matches[ 1 ] ) : 10;
}
}
if ( $query ) {
$the_posts = get_posts( array(
'category' => $item->object_id,
'numberposts' => $showposts
) );
if ( ! empty( $the_posts ) ) {
$new_html[] = '<ul class="sub-menu">';
foreach( ( array ) $the_posts as $post ) {
setup_postdata( $post );
$new_html[] = '<li class="">';
$new_html[] = the_title( '<a href="' . get_permalink( ) . '" class="depth-' . ( $depth ? $depth + 1 : 2 ) . '">', '</a>', false );
$new_html[] = '</li>';
}
$new_html[] = '</ul>';
$item_output .= implode( "\n", $new_html );
}
}
}
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'menu_show_media_post', 10, 4 );