Is there a way to add a condition to know if the current item of the navigation walker is the last child of the menu or parent?

| Item 1      | Item 2      |
| - Item 1.1  | - Item 2.1  |
| - Item 1.2  | - Item 2.2  |
| - Item 1.3  |<----------------- Determine if current item is last child 

For example:

if ($item->last_child(of_current_parent) {

So, how do you determine if the current item is the last child of its siblings?

EDIT: Solved my problem trying this: https://wordpress.stackexchange.com/a/49005/91164 But had to change the logic quite a bit to fit my needs. See my answer below.


The code below is not tested but is a good start for what you need. You will need to have the Walker class declared on you theme and then extended on your below function .

class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_el(&$output, $item, $depth, $args) {

  if($args['has_children']) {
  // I know what to do here

  • This one works for me if($args->walker->has_children){ ... } and I'm currently using it. But what I'm looking for is how to determine if the item is the last child of its siblings. Just edited my post. Sorry if you misunderstood it. But this is my case right now. – dbybanez Aug 7 '17 at 10:31

Okay, so I solved my problem using this link: https://wordpress.stackexchange.com/a/49005/91164 The example only shows how to add classes to the last child of its siblings. But want I want to do is to modify how the last child displays.

Though this is the closest to what I'm looking for, I just had to modify some logics of this code.

I also encountered some problems displaying the item title. What I did was, instead of using $item, I used $element in function display_element(). For example (retrieving the item's url): $element->url instead of $item->url.

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.