Skip to content

Instantly share code, notes, and snippets.

@dospuntocero
Last active January 4, 2020 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dospuntocero/040b3a6c9b00917339cfe9f2be6d11a2 to your computer and use it in GitHub Desktop.
Save dospuntocero/040b3a6c9b00917339cfe9f2be6d11a2 to your computer and use it in GitHub Desktop.
from time to time you need to completely change the way a menu markup is displayed and the walker class is a pain. you can just use this procedural function and add classes or markup you need
function clean_custom_menus() {
$menu_name = 'nav-primary'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>';
$menu_list .= '<ul>';
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li><a href="'. $url .'">'. $title .'</a></li>';
}
$menu_list .= '</ul>';
$menu_list .= '</nav>';
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
it will return something like this
<nav>
<ul>
<li><a href="http://example.com/">Home</a></li>
<li><a href="http://example.com/demos/">Demos</a></li>
<li><a href="http://example.com/downloads/">Downloads</a></li>
<li><a href="http://example.com/docs/">Documentation</a></li>
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment