Skip to content

Instantly share code, notes, and snippets.

@deshabhishek007
Created June 28, 2018 18:04
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 deshabhishek007/cb533ef6f68f08294a50d7cbc7322265 to your computer and use it in GitHub Desktop.
Save deshabhishek007/cb533ef6f68f08294a50d7cbc7322265 to your computer and use it in GitHub Desktop.
JS based raw menu for Smarty Templates - https://github.com/deshabhishek007/wp-menu-extractor
<div id="menu_container"></div>
{literal}
<script>
var json_path = "https://DOMAIN.HERE/wp-json/wp-menu-extractor/v2/menus/IDHERE";
var json_menu, menu_items, menu_markup;
fetch(json_path)
.then(json_menu => json_menu.json())
.then(json_menu =>{
menu_items = json_menu.items;
menu_markup = menu(menu_items);
var menu_el = document.getElementById('menu_container');
menu_el.innerHTML = menu_markup;
})
function menu(menu_items){
var markup = "";
markup +='<ul>';
menu_items.forEach(function(item){
markup += '<li><a href="'+item.url+'" class="'+item.classes+'" target="'+item.target+'">'+item.title+'</a>';
if (item.children) {
markup += menu(item.children);
}
markup += '</li>';
});
markup +='</ul>';
return markup;
}
</script>
{/literal}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment