• Resolved g3legacy

    (@g3legacy)


    Hi, im trying to get select bits of info out of wp-nav_menu. I have a menu made in the Admin UI area thats needs different classes adding to each li tag. I can’t seem to get a foreach loop to work. The other thing I need it for is from a drop down selection box. I want to do a foreach on the name of each page in a given menu.

    Any ideas how i return the name and permalink or whatever of each Li? Or any suggestions on looping through the menu itself? I’ve tried dropping it into a variable with the echo set to false. When i loop though it doesn’t work, maybe because its an object? My PHP is a little limited right now…

    Help : )

Viewing 11 replies - 1 through 11 (of 11 total)
  • It is a little hard to give specific details, but you can capture the output of wp_nav_menu in a variable (as you seem to have done) and then use preg_match_all to extract the links:

    $menu = wp_nav_menu(array('echo' => false));
    if (preg_match_all('#(<a [^<]+</a>)#',$menu,$matches)) {
       foreach ($matches[0] as $link) {
          // Do something with the link
          echo '<p>',htmlentities($link),'</p>';
       }
    }
    Thread Starter g3legacy

    (@g3legacy)

    Thank you very much for taking the time to write.

    Thats a lot closer than I have got, I haven’t heard of preg_match_all. I’ve been looking at it on PHP.net – i’m trying to work out what the first argument does, or how you choose what goes in it. You have #(<a>)# – does that tell the PHP to look for those elements or something?

    So far your code has returned a link with html round it, and the link in the middle. Do you know how i take just the link out? Or just the name of the page?

    Thanks again, Dan

    can you not add these css classes to the links in the menu admin?
    https://img545.imageshack.us/img545/4026/menucss.jpg

    maybe you need to change the screen options first?
    https://img143.imageshack.us/img143/2097/menuclass.jpg

    Thread Starter g3legacy

    (@g3legacy)

    I have looked into it, but i’m trying to make things in as “user-frendly” was as I can. By doing the stuff I said about in the first post, i’m trying to use the great front end in the Admin panel to gain a list of links from the user. Then, i’d like to do some other stuff with it.

    One of things I want to do its add a span with an individual class to each li in my main nav. If i can put it through a loop i think i can do this. That way I have some unique identifiers for JQuery to target. I can’t see another way to attach JQuery to single A tags in the navigation…..you welcome to make other suggestions though.

    Dan

    Thread Starter g3legacy

    (@g3legacy)

    …and thank you for taking the time to do the images alchymyth

    Not sure if you want just the URL, or the href= part, so I left the href in:

    $menu = wp_nav_menu(array('echo' => false));
       if (preg_match_all('#(<a [^<]+</a>)#',$menu,$matches)) {
          $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/';
          foreach ($matches[0] as $link) {
             // Do something with the link
             if (preg_match($hrefpat,$link,$hrefs)) {
                $href = $hrefs[1];
             }
             if (preg_match('#>([^<]+)<#',$link,$names)) {
                $name = $names[1];
             }
             echo "<p>Name: $name   $href</p>";
          }
       }
    Thread Starter g3legacy

    (@g3legacy)

    vtxyzzy, that is fantastic! It generates the name and link! I’ve spent some time looking though the info on preg_match_all today, its looks like a very useful function. I now need to figure out how to add the arguments I want like you have. I can see some of the characters are escaped? I think that’s the correct term for it. If you have the time or patience to explain, how did you know what to place in the $hrefpat variable? The * looks for anything either side of the equals sign I believe, but what about the crazy looking stuff in brackets?

    Thanks again ??

    Hold your breath! Here is the blow by blow explanation:

    '/(href *= *([\"\']?)([^\"\' ]+)\2)/';

    '/ Start the pattern

    ( Begin saving characters in $matches[1]

    href Find href

    * Followed by zero or more spaces

    = Followed by =

    * Followed by zero or more spaces

    ( Begin saving characters in $matches[2]

    [\"\']? Followed by an optional quote mark

    ) End saving in $matches[2]

    ( Begin saving in $matches[3]

    [^\"\' ]+ Match 1 or more characters not a quote or space

    ) End saving in $matches[3]

    \2 Followed by what is now in $matches[2]

    ) End saving in $matches[1]

    /' End the pattern

    Thread Starter g3legacy

    (@g3legacy)

    i’ve literally read that 10 times through to try and grasp it haha. Its looks logical…i think….

    Am I correct in thinking matches [3] holds the actual link? Its looks like 1 gets the href, 2 gets the quote (and then puts a closing quote at the end too) – so that leaves the link itself.

    Also, in the code you wrote for the name match, its starts with a hash and a greater than, does the hash mean link?

    This stuff is fantastic, thanks for writing it for me. Im going to copy and save all of it….

    [3] holds the URL that the href refers to inside the <a tag to give a complete link.

    The hash is an alternate way to start a pattern. You can use any one of a number of characters as long as you start and end the pattern with the same character. I usually use a slash ‘/’, or a hash ‘#’.

    Thread Starter g3legacy

    (@g3legacy)

    Ok, thats makes a reasonable amount of sense to me. You have been extremely helpful and i’m grateful for your time. I’m going to see what strange results I can get now. I’m starting to quite like PHP….

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Trying to get items out of wp_nav_menu’ is closed to new replies.