在菜单中查找活动菜单项

时间:2017-05-01 16:42:19

标签: wordpress

所以我想我会重写菜单生成器,根据用户的位置对菜单进行重新排序,我想出了一种移动它的方法,这是相对粗糙的方式。

但我的问题是,如何查找当前活动选项的名称或ID。

下面的示例使用硬编码值($ current_id)来标识要放在中间的内容,但我想获取当前页面的ID。

这是重新排序列表的核心

    $menuLocations = get_nav_menu_locations(); // Get our nav locations (set in our theme, usually functions.php)
                                               // This returns an array of menu locations ([LOCATION_NAME] = MENU_ID);

//    foreach ( $menuLocations as $menuLocation ) {
//      echo '<p style="color: red">',$menuLocation,'</p>';

//     }
//     print_r(array_keys($menuLocations));

    $menuID = $menuLocations['primary_navigation']; // Get the *primary* menu ID
    $primaryNav = wp_get_nav_menu_items($menuID); // Get the array of wp objects, the nav items for our queried location.

//print_r(array_keys($primaryNav));



    $current_id = 5;
    $menu_length = count($primaryNav);
    if ( '5' == $menu_length ){
        echo $MenuStart;
        for ($i = 0 ; $i <=4 ; $i++) {
            $cu_target = $i + $current_id + 3;
            if ($cu_target > 5 ) {
              $cu_target -= 5;
            }
            if ($cu_target > 5 ) {
               $cu_target -= 5;
            }
            $navItem = $primaryNav[$cu_target - 1];
            echo '<li class="MainNavLink',$i,' nav-item">';
            echo '<a class="nav-link py-1 pr-3" href="'.$navItem->url.'" title="'.$navItem->title.'">'.$navItem->title.'</a>';
            echo '</li>';
        }

1 个答案:

答案 0 :(得分:0)

最后,围绕这个

有一个相对简单的方法
$thispost = get_post();
$menu_order = $thispost->menu_order;
$current_id = $menu_order;
if ( '0' == $current_id ){
   $current_id = 1;
}

不确定为什么它会为第一页报告0,并且它确实要求您已经下订单(至少我正在使用它)但除此之外,它会以数字方式发回menu_order,因此您可以玩菜单:)

相关问题