Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:06
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save ajschlosser/c69b9e52c25ddd5f7616 to your computer and use it in GitHub Desktop.
Wraps iterated wp_get_nav_menu_items output in customizable HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function print_wp_nav_menu ($menu, $params=array()) { | |
/** | |
* | |
* Wraps iterated wp_get_nav_menu_items output in customizable HTML | |
* | |
* @author Aaron John Schlosser <aaron@aaronschlosser.com> | |
* @link https://gist.github.com/ajschlosser/daf17648f0f42d1597ec#file-acf_make_two_columns | |
* @copyright Copyright 2014 Aaron John Schlosser | |
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2 | |
* @param array $params Array containing all other parameters (optional) | |
* @return null | |
*/ | |
if (!$params || gettype($params) != "array") { | |
$params = array(); | |
$params["menu_class"] = "menu"; | |
$params["submenu_class"] = "dropdown"; | |
$params["first_item_class"] = "first"; | |
$params["last_item_class"] = "last"; | |
$params["every_item_class"] = "menuizer"; | |
$params["every_submenu_item_class"] = "menuizer-submenu"; | |
$params["parent_item_class"] = "has-dropdown"; | |
$params["parent_item_end"] = "<span class='caret'></span>"; | |
$params["close_tag"] = "</li>"; | |
$params["framework"] = "foundation"; | |
$params["items_per_column"] = 0; | |
$params["column_class"] = "small-12 large-12 columns"; | |
} | |
if (!strcmp($params["framework"], "bootstrap")) { | |
$params["menu_class"] = "nav navbar-nav"; | |
$params["column_class"] = "col-lg-12"; | |
$params["parent_item_class"] = "dropdown"; | |
$params["submenu_class"] = "dropdown-menu"; | |
} | |
if ($params && gettype($params) === "array") { | |
if (!$params["menu_class"]) $params["menu_class"] = "menu"; | |
if (!$params["submenu_class"]) $params["submenu_class"] = "dropdown"; | |
if (!$params["first_item_class"]) $params["first_item_class"] = "first"; | |
if (!$params["last_item_class"]) $params["last_item_class"] = "last"; | |
if (!$params["every_item_class"]) $params["every_item_class"] = "menuizer"; | |
if (!$params["every_submenu_item_class"]) $params["every_submenu_item_class"] = "menuizer-submenu"; | |
if (!$params["parent_item_class"]) $params["parent_item_class"] = "has-dropdown"; | |
if (!$params["close_tag"]) $params["close_tag"] = "</li>"; | |
if (!$params["framework"]) $params["framework"] = "foundation"; | |
if (!$params["items_per_column"]) $params["items_per_column"] = 0; | |
if (!$params["column_class"]) $params["column_class"] = "small-12 large-12 columns"; | |
} | |
$framework = $params["framework"]; | |
$items = wp_get_nav_menu_items($menu); | |
$l = count($items); | |
$menu_class = $params["menu_class"]; | |
$submenu_class = $params["submenu_class"]; | |
$first_item = true; | |
$first_child = true; | |
$parent_item_class = ""; | |
$parent_item_end = ""; | |
$close_tag = $params["close_tag"]; | |
$every_item_class = "" . $params["every_item_class"]; | |
$every_submenu_item_class = "" . $params["every_submenu_item_class"]; | |
$first_item_class = " " . $params["first_item_class"]; | |
$last_item_class = " " . $params["last_item_class"]; | |
$items_per_column = $params["items_per_column"]; | |
$column_class = $params["column_class"]; | |
$bootstrap_header = ""; | |
$bootstrap_dropdown_code = ""; | |
$foundation_header = ""; | |
$begin_row = ""; | |
$end_row = ""; | |
$begin_column = ""; | |
$end_column = ""; | |
if (!strcmp($framework, "bootstrap")) { | |
$bootstrap_header = "<!--Begin Bootstrap-Specific Header--><div class='container-fluid'><div class='navbar-header'><button type='button' class='navbar-toggle collapsed' data-toggle='collapse' data-target='#menuizer-nav'><span class='icon-bar'></span><span class='icon-bar'></span><span class='icon-bar'></span><a class='navbar-brand' href='#'></a></div><div class='collapse navbar-collapse' id='menuizer-nav'><!--End Bootstrap-Specific Header-->"; | |
$bootstrap_dropdown_code = "class='dropdown-toggle' data-toggle='dropdown'"; | |
} | |
if (!strcmp($framework, "foundation")) { | |
$foundation_header = "<div class='row'>"; | |
} | |
if ($items_per_column > 0) { | |
$begin_row = "<div class='row'>"; | |
$end_row = "</div>"; | |
$begin_column = "<div class='".$column_class."'>"; | |
$end_column = "</div>"; | |
} | |
echo "<!--Begin Automatically Generated Menu-->".$bootstrap_header."<ul class='".$menu_class."'>".$begin_row; | |
for ($i = 0; $i < $l; $i++) { | |
// Check to see if the next item is a child. | |
// If the next item is a child, then this one is the parent | |
if ($i < $l-1 && $items[$i+1]->menu_item_parent > 0) { | |
$parent_item_class = $params["parent_item_class"]; | |
$parent_item_end = $params["parent_item_end"]; | |
$close_tag = ""; | |
} else { | |
$parent_item_class = ""; | |
$parent_item_end = ""; | |
$close_tag = $params["close_tag"]; | |
} | |
$item = $items[$i]; | |
// Do this if the item is in a submenu | |
if ($item->menu_item_parent > 0) { | |
// If it's the _first_ item in the submenu, start the new sub menu before it | |
if ($first_child) { | |
echo "<!--Begin Automatically Generated Sub Menu--><ul class='".$submenu_class." menuizer'>"; | |
$first_child = false; | |
} | |
// Otherwise, add it to the already-existing submenu | |
echo "<li><a href='".$item->url."' class='".$every_submenu_item_class." menuizer'>".$item->title."</a>".$params["close_tag"].""; | |
} | |
// Do this if the item is not in a submenu | |
else { | |
// If it's the first item after a submenu, close the submenu here | |
if (!$first_child) { | |
echo "</ul><!--End Automatically Generated Sub Menu-->"; | |
$first_child = true; | |
} | |
// Do this if there are columns, and it is the end of a column | |
if ($items_per_column > 0) { | |
if ($i % $items_per_column === 0 && $i !== 0) { | |
echo "</div><div class='".$column_class."'>"; | |
} | |
} | |
// Do this if it is the first item | |
if ($i === 0) echo $begin_column."<li class='".$parent_item_class." menuizer'><a href='".$item->url."' class='".$every_item_class."".$first_item_class."'>".$item->title.$dropdown_code."</a>".$close_tag.""; | |
// Do this if it is the last item | |
else if ($i === $l-1) echo "<li class='".$parent_item_class."' menuizer><a href='".$item->url."' class='".$every_item_class."".$last_item_class."'>".$item->title.$dropdown_code."</a>".$close_tag."".$end_column; | |
// Otherwise, just add another item to the menu | |
else { | |
// But first, see if it isn't in a submenu | |
if (!$parent_item_end) echo "<li class='".$parent_item_class." menuizer'><a href='".$item->url."' class='".$every_item_class."'>".$item->title.$parent_item_end."</a>".$close_tag.""; | |
// If it is, do this | |
else echo "<li class='".$parent_item_class." menuizer'><a href='#' class='".$every_item_class." ".$parent_item_class."' ".$bootstrap_dropdown_code."'>".$item->title.$parent_item_end."</a>".$close_tag.""; | |
} | |
} | |
} | |
$bootstrap_close_tag = ""; | |
if (!strcmp($framework, "bootstrap")) { $bootstrap_close_tag = "</div></div>"; } | |
echo $end_row."</ul><!--Begin Boostrap-Specific Closing Tag-->".$bootstrap_close_tag."<!--End Boostrap-Specific Closing Tag--><!--End Automatically Generated Menu-->"; | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example implementation with default parameters (optimized for Zurb Foundation):
With user-defined parameters:
Or in Jade, for example:
- print_wp_nav_menu("menu_left", array("menu_class"=>"sidenav"));
Enjoy.