dw-hyphen
1/28/2015 - 4:23 PM

Insert a logo into the centre of a Wordpress Menu

Insert a logo into the centre of a Wordpress Menu

/**
 * Add logo to center of main navigation menu
 */

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

function add_custom_in_menu( $items, $args ) {
  if( $args->theme_location == 'primary' ) { // inserts custom item into primary menu
    $items_array = array();

    while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) ) {
     $items_array[] = substr($items, 0, $item_pos);
     $items = substr($items, $item_pos);
    }

    $items_array[] = $items;

    $logo = hyphenated_custom_logo_url();
    $insert = '<li class="logo"><img src="' .  $logo . '"></li>';

    array_splice($items_array, 2, 0, $insert); // insert custom item after 2nd one

    $items = implode('', $items_array);
  }
  return $items;
}