Support » Fixing WordPress » wp_nav_menu order number

  • Resolved leapin_leprechaun


    hey just wonderin if anyone has come across a way to call the order number of nav items in wp_nav_menu?

    so for eg if its first in the menu you get back 1, 2nd get back 2 etc..

    was gonna try to include it in the id/class of each item in the navigation if its there to use

    It may not even be set up like that, just a thought i had, sed I’d check if any1 knows otherwise 1st!


Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael


    Forum Moderator

    for instance in the deafult TwentyTen, in header.php;

    from this:

    <?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>

    using the ‘echo’ parameter of ‘wp_nav_menu()’ and some string manipulation, to this:

    $navvi = wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header', 'echo' => FALSE ) ); 
    $nav_bits = explode('<li ', $navvi);
    $navvi = ''; $i = 0;
    foreach($nav_bits as $bits) :
    if($i==0) { $navvi = $navvi.$bits; }
    else { $navvi = $navvi.'<li class="item'.$i.'" '.$bits; }
    echo $navvi;

    (should work, only tested with my wp3 install on TwentyTen theme; same principle as in my reply to your other thread, but slightly different in the treatment of the first fragment.)

    i am just curious, what is the advantage to know the position of the list item, particular in multilevel lists?

    Hi again alchymyth and thanks again for your time

    yea i had used the other piece of code you gave me, which works perfectly on wp_list_pages. I altered it very similarly to what you have done here but it doesn’t work in the same way on wp_nav_menu for some reason.. i’ll hav a look through twenty10, maybe theres a function i’m missing that could help out. you said it worked for you on twenty10 yea?

    It displays the menu alright, so the code is fine, but there is no effect on the class at all, it just displays classes as if I displayed the menu normally.

    the position of the item was just another way around what you did the first time to be honest, and its cosmetics the reason i’m drivin to such madness!! 😀

    thanks again for your time alchymyth, really appreciate it.


    have put in any functions from the twenty10 theme that are associated with wp_nav_menu but no luck 🙁

    actually alchymyth apologies, it works perfectly! i went through the code again and noticed a mistake i made myself! d’oh!

    thanks again, ur my WP saviour!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_nav_menu order number’ is closed to new replies.