0

In my website I need a navigation menu to show arrangement of my posts. So I need the navigation menu to count the items and display the number of every item as a label before the title of that item. I think we need a foreach in wp_get_nav_menu_items to count but I don't know exactly the way.
I need something like this from that menu:
1- title one
2- title two
3- title three
4- ...

I have the following codes but I have two issues:
1- I do not know why it does not work?
2- I need a class as .current-menu-item to find the current item in other items. this class is as a default class in wordpresss menu but I can not do that.

// 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

Thanks a lot to helping

0

Declare menu_count = 0; before your foreach and then within your foreach you'll need to include $menu_count++; and then you could add $menu_count as an attribute, or a label.

You will need to make a custom function for this, so be sure to check out the codex on wp_get_nav_menu_items

| improve this answer | |
  • thanks my friend. I updated the question. please check it if you can fix it. thanks again – Sh.Dehnavi Oct 26 '19 at 6:12
  • I can't say for sure without seeing more of your code, but I think you might have it in the wrong place. You need to define your menu in a function and return that menu in order for it to output like you're hoping. You should also be able to snag from the codex the objects that apply the classes you're looking for. I would say You need to take a closer look at this - developer.wordpress.org/reference/functions/… – Faye Oct 30 '19 at 22:28
  • 1
    I wrote the answer. Thanks for all :) – Sh.Dehnavi Nov 1 '19 at 8:44
0

Thanks to all, I found out it myself:
this tested menu supports sub menu, submenu will have arrow. also current page parent and menu item will have active class. you're wellcome :)

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;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.