Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Do you want to generate the single posts sub menu dynamically? What I mean is should it update when a new collection is added or will you be adding them individually yourself? –  sanchothefat Nov 17 '11 at 14:37
    
it should be automatic. the page would be an index/archive page of the custom post entries, and the submenu would be the titles of each entry, linking to an anchor in the same index/archive page instead than an individual page. –  Naoise Golden Nov 18 '11 at 15:15
    
Hi @NaoiseGolden, this is a really interesting question. Did you ever solve this? If so, please post your answer for others to enjoy. If not, let me know and I'll help you work out the solution. –  Matthew Boynes Feb 20 '12 at 22:48
    
Hi @MatthewBoynes I don't recall fixing it (I would've probably answered my own question). Unfortunately I am quite busy now and not working on this any more so I can't help, but would be happy to see an answer for future references. –  Naoise Golden Feb 21 '12 at 15:28
    
Thanks, I'll take a look into it an see if I can come up with something! –  Matthew Boynes Feb 21 '12 at 16:15
add comment

1 Answer

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 );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.