WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_nav_menu order number (5 posts)

  1. leapin_leprechaun
    Member
    Posted 4 years ago #

    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!

    cheers
    leapin

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    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:

    <?php
    $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; }
    $i++;
    endforeach;
    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?

  3. leapin_leprechaun
    Member
    Posted 4 years ago #

    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!! :D

    thanks again for your time alchymyth, really appreciate it.

    cheers

  4. leapin_leprechaun
    Member
    Posted 4 years ago #

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

  5. leapin_leprechaun
    Member
    Posted 4 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic