在我的网站上,我需要一个导航菜单来显示我的帖子安排。因此,我需要导航菜单来计算项目数量,并将每个项目的编号显示为该项目标题之前的标签。我想我们需要一个foreach
在里面wp_get_nav_menu_items
数一数,但我不知道确切的方向<我需要从菜单中找到这样的东西:
1-标题一
2-标题二
3-标题三
4-。。。
我有以下代码,但我有两个问题:
1-我不知道为什么它不工作
2-我需要一门课程.current-menu-item
在其他项目中查找当前项目。这个类是WordPress菜单中的默认类,但我不能这样做。
// Get the nav menu based on $menu_name (same as \'theme_location\' or \'menu\' arg to wp_nav_menu)
// This code based on wp_nav_menu\'s code to get Menu ID from menu slug
$menu_name = \'custom_menu_slug\';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = \'<ul id="menu-\' . $menu_name . \'">\';
menu_count = 0;
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_count++;
$menu_list .= \'<li><span>\'.$menu_count.\'</span><a href="\' . $url . \'">\' . $title . \'</a></li>\';
}
$menu_list .= \'</ul>\';
} else {
$menu_list = \'<ul><li>Menu "\' . $menu_name . \'" not defined.</li></ul>\';
}
// $menu_list now ready to output
非常感谢你的帮助
最合适的回答,由SO网友:Sh.Dehnavi 整理而成
感谢大家,我自己找到了:
这个测试菜单支持子菜单,子菜单将有箭头。此外,当前页面父项和菜单项将具有活动类。你很好:)
function our_custom_menu( $menu_name ) {
//$menu_name = \'header-menu\';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = \'\';
$count = 0;
menuitem_count == 0;
$submenu = false;$cpi=get_the_id();
foreach( $menu_items as $current ) {
if($cpi == $current->object_id ){if ( !$current->menu_item_parent ) {$cpi=$current->ID;}else{$cpi=$current->menu_item_parent;}$cai=$current->ID;break;}
}
foreach( $menu_items as $menu_item ) {
$link = $menu_item->url;
$title = $menu_item->title;
$menuitem_count++;
$menu_item->ID==$cai ? $ac2=\' current_menu\' : $ac2=\'\';
if ( !$menu_item->menu_item_parent ) {
$parent_id = $menu_item->ID;$parent_id==$cpi ? $ac=\' current_item\' : $ac=\'\';
if(!empty($menu_items[$count + 1]) && $menu_items[ $count + 1 ]->menu_item_parent == $parent_id ){ //Checking has child
$menu_list .= \'<li class="dropdown has_child\'.$ac.\'"><span>\'.$menuitem_count.\'</span><a href="\'.$link.\'" class="dropdown-toggle\'.$ac2.\'" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="nav-span"></span>\'.$title.\'<span class="caret"></span></a>\';
}else{
$menu_list .= \'<li class="\'.$ac.\'">\' ."\\n";$menu_list .= \'<span>\'.$menuitem_count.\'</span><a href="\'.$link.\'" class="\'.$ac2.\'">\'.$title.\'</a>\' ."\\n";
}
}
if ( $parent_id == $menu_item->menu_item_parent ) {
if ( !$submenu ) {
$submenu = true;
$menu_list .= \'<ul class="dropdown-menu">\' ."\\n";
}
$menu_list .= \'<li class="item">\' ."\\n";
$menu_list .= \'<a href="\'.$link.\'" class="\'.$ac2.\'">\'.$title.\'</a>\' ."\\n";
$menu_list .= \'</li>\' ."\\n";
if(empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu){
$menu_list .= \'</ul>\' ."\\n";
$submenu = false;
}
}
if (empty($menu_items[$count + 1]) || $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) {
$menu_list .= \'</li>\' ."\\n";
$submenu = false;
}
$count++;
}
} else {
$menu_list = \'<li>Menu "\' . $menu_name . \'" not defined.</li>\';
}
echo $menu_list;
}