Determine the last child of the parent menu WP Nav Walker

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.

Topic walker navigation menus Wordpress

Category Web


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.


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


  }
 }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.