jrobinsonc
8/10/2016 - 1:26 AM

Wordpress helper: Show menu

Wordpress helper: Show menu

<?php

/**
 * show_menu
 *
 * @author JoseRobinson.com
 * @link https://gist.github.com/jrobinsonc/932b7b7d3b724ac4be0399a81e1b3c08
 * @version 1.0.0
 * @param string $location
 * @param array $args
 * @return string
 */
function show_menu($location, $args = [])
{
    $def_args = array_merge([
        'theme_location' => $location,
        'container' => 'nav', // Here we are using NAV tag as container.
        'container_class' => $location . '-menu menu clearfix', //
    ], $args);

    $def_args['echo'] = false;

    $menu = wp_nav_menu($def_args);

    // WordPress adds a generic ID to the container,
    // this code removes this ID if it was not specified in the arguments.
    if (! isset($args['menu_id']))
        $menu = preg_replace('#<ul(.+)id="[^"]+"(.+)>#mU', '<ul$1$2>', $menu);

    $menu = preg_replace('#<a(.+)href="(/[^"]+)"(.*)>#U', '<a$1href="'. get_bloginfo('url') .'$2"$3>', $menu);

    echo $menu;
}