我有一个这样的数组:
Array
(
[0] => stdClass Object
(
[ID] => 1
[menu_item_parent] => 0
[title] => Home
[url] => http://www.example.com/
)
[1] => stdClass Object
(
[ID] => 2
[menu_item_parent] => 0
[title] => Menu 2
[url] => http://www.example.com/menu-2/
)
[2] => stdClass Object
(
[ID] => 3
[menu_item_parent] => 2
[title] => Sub Menu 1
[url] => http://www.example.com/menu-2/sub-menu-1
[target] =>
)
[3] => stdClass Object
(
[ID] => 4
[menu_item_parent] => 0
[title] => Menu 4
[url] => http://www.example.com/menu-4/
[target] =>
)
)现在你可以看到数组的第三个项是第二个数组项的子项(请参阅列menu_item_parent).Now我的问题是如何使用这个array.Please帮助来显示这个父项及其子项。
发布于 2013-03-12 22:50:02
最后,在@Matt.C的帮助下解决我的问题,给@Matt.C提供link.Thanks。解决方案如下:
首先以平面数组的形式获取菜单项:
<?php
$menu_name = 'main_nav';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );
?>然后迭代菜单项的数组:
<nav>
<ul class="main-nav">
<?php
$count = 0;
$submenu = false;
foreach( $menuitems as $item ):
// get page id from using menu item object id
$id = get_post_meta( $item->ID, '_menu_item_object_id', true );
// set up a page object to retrieve page data
$page = get_page( $id );
$link = get_page_link( $id );
// item does not have a parent so menu_item_parent equals 0 (false)
if ( !$item->menu_item_parent ):
// save this id for later comparison with sub-menu items
$parent_id = $item->ID;
?>编写第一个父项<li>:
<li class="item">
<a href="<?php echo $link; ?>" class="title">
<?php echo $page->post_title; ?>
</a>
<a href="<?php echo $link; ?>" class="desc">
<?php echo $page->post_excerpt; ?>
</a>
<?php endif; ?>检查此项目的父id是否与存储的父id匹配:
<?php if ( $parent_id == $item->menu_item_parent ): ?>
Start sub-menu <ul> and set $submenu flag to true for later referance:
<?php if ( !$submenu ): $submenu = true; ?>
<ul class="sub-menu">
<?php endif; ?>
Write the sub-menu item:
<li class="item">
<a href="<?php echo $link; ?>" class="title"><?php echo $page->post_title; ?></a>
<a href="<?php echo $link; ?>" class="desc"><?php echo $page->post_excerpt; ?></a>
如果下一项没有相同的父id,并且我们声明了一个子菜单,那么关闭子菜单<ul>
<?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?>
</ul>
<?php $submenu = false; endif; ?>
<?php endif; ?>同样,如果数组中的下一项不具有相同的父id,请关闭<li>
<?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ): ?>
</li>
<?php $submenu = false; endif; ?>
<?php $count++; endforeach; ?>
</ul>
</nav> 发布于 2013-03-12 21:10:50
试试这个:我将输入添加为数组,根据您的问题更改为对象。
$array = Array( array("ID" => 1,"menu_item_parent" => 0,"title" => "Home","url" => "http://www.example.com/"),
array("ID" => 2,"menu_item_parent" => 0,"title" => "Menu 2","url" => "http://www.example.com/menu-2/"),
array("ID" => 3,"menu_item_parent" => 2,"title" => "Sub Menu 1","url" => "http://www.example.com/menu-2/sub-menu-1","target" =>"" ),
array("ID" => 4,"menu_item_parent" => 0,"title" => "Menu 4","url" => "http://www.example.com/menu-4/","target" => "")
);
$res = array();
foreach($array as $val){
if($val['menu_item_parent'] != 0){
$res[$val['menu_item_parent']]['child'][] = $val;
}
else{
$res[$val['ID']] = $val;
}
}
echo "<pre>";
print_r($res);输出:
Array
(
[1] => Array
(
[ID] => 1
[menu_item_parent] => 0
[title] => Home
[url] => http://www.example.com/
)
[2] => Array
(
[ID] => 2
[menu_item_parent] => 0
[title] => Menu 2
[url] => http://www.example.com/menu-2/
[child] => Array
(
[0] => Array
(
[ID] => 3
[menu_item_parent] => 2
[title] => Sub Menu 1
[url] => http://www.example.com/menu-2/sub-menu-1
[target] =>
)
)
)
[4] => Array
(
[ID] => 4
[menu_item_parent] => 0
[title] => Menu 4
[url] => http://www.example.com/menu-4/
[target] =>
)
)发布于 2014-07-09 01:50:56
下面是一个非常简单的类,用于解决Wordpress特有的问题,其中包含一个返回所有子菜单项的get_submenu函数:
class NestedMenu
{
private $flat_menu;
public $items;
function __construct($name)
{
$this->flat_menu = wp_get_nav_menu_items($name);
$this->items = array();
foreach ($this->flat_menu as $item) {
if (!$item->menu_item_parent) {
array_push($this->items, $item);
}
}
}
public function get_submenu($item)
{
$submenu = array();
foreach ($this->flat_menu as $subitem) {
if ($subitem->menu_item_parent == $item->ID) {
array_push($submenu, $subitem);
}
}
return $submenu;
}
}用法,构造实例:
$menu = new NestedMenu('menu_name');迭代:
foreach ($menu->items as $item) { ...并获取循环内的子菜单:
$submenu = $menu->get_submenu($item);在显示子菜单之前,您可以检查它是否存在:
if ($submenu): ...https://stackoverflow.com/questions/15361905
复制相似问题