Forums

Need to change the output of wp_nav_menu (4 posts)

  1. asteffensen
    Member
    Posted 1 year ago #

    Hello!

    I’ve been searching for a solution for days now, but I just can’t seem to find anything.. I’m working on a new theme, and I need to change the output of wp_nav_menu. I know this is done by creating a custom walker, however I am not familiar enough with PHP to create something like that.

    What I need the wp_nav_menu to do, is this:

    • Show at least two levels, however, only when the parent element is active.
    • Elements from each level needs to be contained within a div, preferably with a corresponding class name. Top-level elements in one div, and child elements in another.

    If someone has already written a custom walker to do something like this, or would be kind enough to help me write a new one, that would’ve been awesome! You’d be helping a quite desperate man :)

    Thanks in advance!

  2. idealien
    Member
    Posted 1 year ago #

    Although it doesn't answer your specific question - this example of a custom walker was a great starting point for me in making specific output of custom fields show up in a menu based on post type.

  3. tapdance
    Member
    Posted 1 year ago #

    Hi ! I have started making a barebone test theme using header.php, home.php, footer.php and functions.php .

    I looked around, found several examples for walker function and created a barebone example.

    The only problem i have is that newline \n is not properly escaped. I do not know why. I 've look at function end_el() that also has \n which IS properly processed. I don't know if this is a bug in PHP executable in my WAMP installation.

    Anyway, look here for example.

  4. tapdance
    Member
    Posted 1 year ago #

    About problems escaping characters - i stand corrected. To have \t \n and other characters properly escaped, they need to be enclosed in double quotes. These troubles are what i get for not learning basic PHP ...

    But, sometimes that is not an option. Example would be 'items_wrap' option passed in array() argument for wp_nav_menu(). It has to enclosed in single quotes else function will fail to output anything.

    String concatenation with for example "\t" or "\n" will work.

Topic Closed

This topic has been closed to new replies.

About this Topic