/**
 * Inject site logo to the beginning of a menu.
 *
 * @param string   $items     Menu items.
 * @param stdClass $menu_args Menu arguments.
 *
 * @return string
 */
function dorzki_inject_logo_to_menu( $items, $menu_args ) {

  if( 'header_menu' === $args->theme_location ) {

    $items = "<li>"
		     . "    <a href='" . get_bloginfo( 'url' ) . "'>"
		     . "	    <img src='" . get_theme_file_uri( 'assets/images/header-logo.png' ) . "' alt='" . esc_attr( get_bloginfo( 'name' ) ) . "'>"
		     . "	</a>"
		     . "</li>"
		     . $items;
    
  }

  return $items;
  
}

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

How to use it?

Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php file at the end of the file, just before ?>.

For JavaScript code you will need to add them to your theme’s main JavaScript file.

Having issues with the code?

If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.

Comments

Leave a Reply