Make it easier to use definition lists with wp_nav_menu

  1. Nick

    I may be missing something but I noticed something that I think can be improved. Currently, it looks like if a developer wants to output a list using wp_nav_menu, they are stuck with unordered lists like this:


    What if the developer thinks that the appropriate markup should be a definition list:


    Since there are only 3 kinds of lists in HTML, it seems that wp_nav_menu ought to have built-in support for <dl> rather than relying on developers to write custom walker classes each time.

    Posted: 8 years ago #
  2. davidpaulsson

    I'd +1 this if I could. Or did I just do that? Nvm, good idea. You have my full support.

    Posted: 8 years ago #
  3. MayerRubin

    I know its a bit late, but maybe this will help the next person with the same problem.

    I got it to do what you wanted by a combination of function arguments (echo and items_wrap) and processing the output of wp_nav_menu() using the PHP function str_replace().

    $args = array(
      'echo' => false,
      'items_wrap' => '<dl id="%1$s" class="%2$s myclass">%3$s</dl>',
    $subject = wp_nav_menu( $args ); 
    $search  = array('<ul', '</ul>', '<li', '</li>');
    $replace = array('<dl', '</dl>', '<dd', '</dd>');
    echo str_replace($search, $replace, $subject);

    You can replace the <ul> tag either in the items_wrap argument or in the str_replace().

    Posted: 7 years ago #
  4. Commentguerir

    @mayerrubin Thanks for this piece of code I did not know that we can use str_replace() to replace the generated code like that ^^

    Posted: 5 years ago #

RSS feed for this topic


You must log in to post.

  • Rating

    6 Votes
  • Status

    This idea is under consideration