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'm working on a custom menu that is particularly complex and I like to know which is the right way to build it.

This is the structure of my menu:

- Home (is a PAGE)

- Information (Is a PAGE with a second level menu)
        |_________Items are the post for the category "information"

- Data (It hasn't a PAGE, only a second level menu)
        |_________Items are the posts for the category "data"

In your opinion which is the best way to build a menu like this? is it possible to build it from the WP backend or is better to create it just from code in the template?

share|improve this question
add comment

1 Answer

It is possible to do this using the backend nav menu support, using classes as markers that are then picked via the walker_nav_menu_start_el filter ( which then do queries to grab posts and insert their markup )

e.g.

add_filter( 'walker_nav_menu_start_el', 'menu_show_media_post', 10, 4 );

function menu_show_media_post( $item_output = '', $item = '', $depth = '', $args = '' ) {
    global $post;

    $query = false;

    if ( is_array( $item->classes ) ) {

        foreach( $item->classes as $class ) {
            if ( $class == 'some_marker_class_to_watch_for_goes_here') {
                $query = true;
            }
        }
    }

    if ( $query ) {
        $args = array(
        );
        $q = new WP_Query($args);
        if($q->have_posts()){
            while($q->have_posts()){
                $q->the_post();
                // do stuff and append output to $item_output, dont echo it out
            }
        }
        wp_reset_postdata();
    }
    return $item_output;
}

All in all this is a fairly advanced level thing to do.

If you want to go a step further you can add UI controls to the menu pull outs themselves, and store the data as post meta for each menu nav item. This would be a better way of doing it, but it's not for the faint of heart as it requires JS code in the admin backend, and a custom walker to render out the initial html for the nav admin interface.

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.