Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active August 10, 2016 01:28
Show Gist options
  • Save jrobinsonc/932b7b7d3b724ac4be0399a81e1b3c08 to your computer and use it in GitHub Desktop.
Save jrobinsonc/932b7b7d3b724ac4be0399a81e1b3c08 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment