Adding html elements to wp nav menu

I made a dynamic menu using this code:

//add login/logout link to menu

add_filter('wp_nav_menu_items', 'wps_add_login_logout_link', 10, 2);
function wps_add_login_logout_link($items, $args) {
    $login = __('Sign in');
    $logout = __('Sign out');


    $menu_id = '15';
    $menu_id2 = '16'; 

    if ( ! is_user_logged_in() )
        $link = '<a href="' . site_url('log-in-log-out')  . '">' . $login . '</a>';
    else
        $link = '<a href="' . wp_logout_url('log-in-log-out') . '">' . $logout . '</a>';

    if ( $args->menu == $menu_id )
        $items .= '<li>'. $link .'</li>';
    elseif ( $args->menu == $menu_id2 )
        $items .= '<li>'. $link .'</li>';

    return $items;
}

You should be able to grab the $items and $args variable to play with their contents.
I hope this helps

Sorry, I forgot to mention that I was using the ‘Catalyst’ framework, so the way that the $args object is used to grab the menu id may be different in a normal WP site.

Leave a Comment