WordPress.org

Ready to get started?Download WordPress

Forums

Generate custom menu dropdown with wp_nav_menu (4 posts)

  1. mkampitsch
    Member
    Posted 2 years ago #

    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?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    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" ' : '';
  3. mkampitsch
    Member
    Posted 2 years ago #

    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!

  4. vtxyzzy
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic