我'我正在使用数组创建菜单。我'我目前陷入困境,如何显示菜单的所有孩子。以下是代码:
<ul class="nav navbar-nav">
<?php
$get_admin_menu = get_admin_menu();
$get_admin_submenu = get_admin_submenu();
foreach( $get_admin_menu as $menu ){
$mi = 1;
$menu_title = $menu["title"];
$menu_slug = $menu["slug"];
$menu_url = $menu["url"];
echo "<li class=\"dropdown\">";
echo "<a href=\"$menu_url\">";
echo $menu_title;
echo "</a>";
$si = 0;
$total = count($get_admin_submenu);
foreach( $get_admin_submenu as $menu ){
$mi++;
$submenu_title = $menu["title"];
$submenu_slug = $menu["slug"];
$submenu_parent = $menu["parent"];
if( $menu_url == $submenu_parent ){
$si++;
if( $si == 1 ){ echo "<ul class=\"dropdown-menu\" role=\"menu\">"; }
echo "<li>";
echo "<a href=\"$submenu_parent?page=$submenu_slug\">";
echo $submenu_title;
echo "</a>";
echo "</li>";
}
if( $si == 1 ){ echo "</ul>"; break; }
}
echo "</li>";
}
?>
</ul>
**以上代码呈现**
此代码的结果应为
这是代码,如何将菜单和子菜单放入数组中
/* Add Administrator Menu */
$add_admin_menu = array();
function add_admin_menu( $args ) {
global $add_admin_menu;
return $add_admin_menu[] = $args;
}
/* Get Administrator Menu */
function get_admin_menu(){
global $add_admin_menu;
return $add_admin_menu;
}
/* Add Administrator Submenu */
$add_admin_submenu = array();
function add_admin_submenu( $args ) {
global $add_admin_submenu;
return $add_admin_submenu[] = $args;
}
/* Get Administrator Submenu */
function get_admin_submenu(){
global $add_admin_submenu;
return $add_admin_submenu;
}
function recursive_array_search($needle, $haystack) {
foreach($haystack as $key => $value){
$current_key = $key;
if($needle === $value or (is_array($value) and recursive_array_search($needle,$value) !== false)) {
echo $current_key;
}
}
}
function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $k;
}
}
return false;
}
$args = array(
"title" => "Dashboard",
"slug" => "dashboard",
"url" => "dashboard.php"
);
add_admin_menu($args);
$args = array(
"title" => "Media",
"slug" => "media",
"url" => "media.php"
);
add_admin_menu($args);
$args = array(
"title" => "Manage Pages",
"slug" => "pages",
"url" => "pages.php"
);
add_admin_menu($args);
$args = array(
"title" => "Plugin",
"slug" => "plugin",
"url" => "plugin.php"
);
add_admin_menu($args);
$args = array(
"title" => "Users",
"slug" => "users",
"url" => "users.php"
);
add_admin_menu($args);
$args = array(
"title" => "Tools",
"slug" => "tools",
"url" => "tools.php"
);
add_admin_menu($args);
$args = array(
"title" => "Settings",
"slug" => "settings",
"url" => "settings.php"
);
add_admin_menu($args);
/* Media */
$args = array(
"title" => "Media Manager",
"slug" => "media-manager",
"parent" => "media.php"
);
add_admin_submenu($args);
$args = array(
"title" => "Media Settings",
"slug" => "media-settings",
"parent" => "media.php"
);
add_admin_submenu($args);
/* Pages */
$args = array(
"title" => "View All",
"slug" => "view-all",
"parent" => "pages.php"
);
add_admin_submenu($args);
$args = array(
"title" => "Add New",
"slug" => "add-new",
"parent" => "pages.php"
);
add_admin_submenu($args);
$args = array(
"title" => "Template Manager",
"slug" => "template",
"parent" => "pages.php"
);
add_admin_submenu($args);
/* Settings */
$args = array(
"title" => "General",
"slug" => "general",
"parent" => "settings.php"
);
add_admin_submenu($args);
$args = array(
"title" => "Mail Server",
"slug" => "mail-server",
"parent" => "settings.php"
);
add_admin_submenu($args);
$args = array(
"title" => "Security",
"slug" => "security",
"parent" => "settings.php"
);
add_admin_submenu($args);
答案 0 :(得分:1)
在第一个菜单子元素之后中断循环。在循环结束时,你有:
if( $si == 1 ) { echo "</ul>"; break; }
在这里试试这个:
if( $si >= ($total - 1) ) { echo "</ul>"; break; }