以下代码组成了一个自定义菜单:
<ul class="nav navbar-nav">
<?php
$menu = wp_get_nav_menu_items_hierarchically( 'Menu' );
foreach($menu as $toplevel):
$hasChildren = !empty($toplevel['children']);
?>
<li<?php if($hasChildren) echo ' class="dropdown"'; ?>>
<a href="<?php echo $toplevel['url'] ?>"<?php if($hasChildren) echo ' class="dropdown-toggle" role="button" aria-haspopup="true" aria-expanded="false"'; ?>>
<?php echo $toplevel['title'] ?>
</a>
<?php if($hasChildren): ?>
<ul class="dropdown-menu">
<?php foreach($toplevel['children'] as $child): ?>
<li><a href="<?php echo $child['url']; ?>"><?php echo $child['title']; ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php
endforeach;
?>
</ul>
但是此代码不会检索target
属性。因此,如果选中此项:
以上代码将忽略该设置,并将在同一选项卡中保持打开状态。 我应该怎么做以获取菜单项的目标值?我尝试使用$child['target']
,但此字段不存在...
这是wp_get_nav_menu_items_hierarchically
的代码:
function wp_get_nav_menu_items_hierarchically( $menu ) {
$menu_items = wp_get_nav_menu_items( $menu );
$hierarchical_menu = array();
foreach ($menu_items as $item) {
if( !$item->menu_item_parent ) {
$hierarchical_menu[$item->ID]['title'] = $item->title;
$hierarchical_menu[$item->ID]['url'] = $item->url;
$hierarchical_menu[$item->ID]['attr_title'] = $item->attr_title;
} else {
$hierarchical_menu[$item->menu_item_parent]['children'][$item->object_id]['title'] = $item->title;
$hierarchical_menu[$item->menu_item_parent]['children'][$item->object_id]['url'] = $item->url;
}
}
return $hierarchical_menu;
}
答案 0 :(得分:1)
尝试使用get_post_meta($child['ID'],'_menu_item_target',true)
获取菜单项的target属性。
为了能够访问$ child [&#39; target&#39;]属性,您应该通过在 中获取此属性来更改wp_get_nav_menu_items_hierarchically
函数if if else ... 声明:
if( !$item->menu_item_parent ) {
...
$hierarchical_menu[$item->ID]['target'] = $item->tartget;
} else {
...
$hierarchical_menu[$item->menu_item_parent]['children'][$item->object_id]['target'] = $item->target;
}