Customize the menu by adding items

To achieve this, you have to extend the Walker_Nav_Menu class.

First, create a class that extends the Walker_Nav_Menu

/**
 * Custom nav menu.
 * Code copy-pasted from /wp-includes/nav-menu-template.php
 * 
 * @package WordPress
 * @since 3.0.0
 * @uses Walker_Nav_Menu
 */
class User16975_Custom_Menu_Walker extends Walker_Nav_Menu {
    /**
     * @see Walker_Nav_Menu::start_lvl()
     * @since 3.0.0
     *
     * @param string $output Passed by reference.
     */
    function start_lvl(&$output) {}

    /**
     * @see Walker_Nav_Menu::end_lvl()
     * @since 3.0.0
     *
     * @param string $output Passed by reference.
     */
    function end_lvl(&$output) {
    }

    /**
     * @see Walker::start_el()
     * @since 3.0.0
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param object $item Menu item data object.
     * @param int $depth Depth of menu item. Used for padding.
     * @param object $args
     */
    function start_el(&$output, $item, $depth, $args) {
    }
}

Then, implement the start_el function following your needs.

Finally, make WP use your class instead of the default one :

add_filter( 'wp_edit_nav_menu_walker', 'user16975_walker_class');
function user16975_walker_class($class){
    return "User16975_Custom_Menu_Walker";
}

You can also implement end_lvl in your class to append the login link.

Leave a Comment