Support » Fixing WordPress » Generate custom menu dropdown with wp_nav_menu

  • I’m trying to generate a select list with wp_nav_menu. I found this solution, but this adds an <a> tag to the <option> items.
    So I tried wp_get_nav_items() – and modified it to get me a select list. I now have a select list, but I want to add the current-menu-item to the current item like selected="selected".

    How do I achieve this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • First, you need to get the title of the current page. Something like this:

    $current = apply_filters('the_title',$post->post_title);

    Then after you get the title of the item you are processing, you can check it against $current:

    $title = $menu_item->title;
    $selected = ($title == $current) ? ' selected="selected" ' : '';

    Ok cool. I’ll will try that!

    But look: three of my menu items are category-archives. So when I’m on an article page, how do I assign selected="selected" to the category item of my select list?

    Thank you!

    That could be tricky unless all your posts are assigned only a single parent category. If that is true, you should be able to use this:

    $category = get_the_category();
    $title = $category[0]->cat_name;

    Then compare to the menu_item title as before.

    If your posts are in more than one category, or in child categories, this will not work. I do not have a solution for that case.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Generate custom menu dropdown with wp_nav_menu’ is closed to new replies.