Add item to top of menu using a filter in functions.php

I want to add a logo to my main menu. I am using this function:

add_filter( 'wp_nav_menu_items', 'add_logo_to_menu_item', 10, 2 );
function add_logo_to_menu_item ( $items, $args ) {
    if ($args-menu == 'main-menu') {
        $items .= 'lia href="#"img src="PATH_TO_IMAGE"/a/li';
    }

    return $items;
}

The problem I am having is I want the logo to be at the top of the menu. This filter currently places it in the last position of the menu. Any ideas?

Topic menu-order functions php filters menus Wordpress

Category Web


Use $items = 'Blah'.$items instead of $items .= 'Blah'

add_filter( 'wp_nav_menu_items', 'add_logo_to_menu_item', 10, 2 );

function add_logo_to_menu_item ( $items, $args ) {

    if ($args->menu == 'main-menu') {
        $items = '<li><a href="#"><img src="PATH_TO_IMAGE"></a></li>' . $items;
    }

    return $items;

}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.