WordPress Menu: Condition on Parent Item

I have a custom menu that is structured by a parent-child-subchild-and so on hierarchy. On each parent menu item (of which only six exist) there is a special picture I want to show in the corner of the website.
I now need a way to set a condition like this (this is fantasy-code of course):

if (current_parent_menu_item == "Home") : echo "Picture01"; endif;

Read More

if (current_parent_menu_item == "About") : echo "Picture02"; endif;

and so on…

I have no idea how I could access or ask for the current parent menu item.
Can anyone help?

Thanks a lot!
Sebastian

Related posts

Leave a Reply

1 comment

  1. You should be using a walker class for this kind of stuff. Here is a simple walker class that you can use

    class MyWalker extends Walker_Nav_Menu
    {
        function display_element ($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
        {
            // check, whether there are children for the given ID and append it to the element with a (new) ID
            $element->hasChildren = isset($children_elements[$element->ID]) && !empty($children_elements[$element->ID]);
    
            return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
        }
    
        function start_el(&$output, $item, $depth, $args)
        {
            global $wp_query;
    
            // you know can access $item->hasChildren to do anything you want..
        }
    }
    

    If you just want to get over it, then you should use jquery to add the html to the parent element. You can do something like this.

    jQuery(document).ready(){
       jQuery('ul#id > li').prepend('<img src="#" alt="">');
    }