Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

(1) I used the code given on answer to How to use logout function on custom menu link? to programmatically add a menu element to wp_nav_menu_objects (Say "Animals"). I give it an id, say 1000.

(2) Similarly, I succesfully created elements that I wanted to be children of a preexisting element (fixing menu_item_parent to the id of the parent element) (like "Jonquil" to element "Flowers")

(3) The problem comes when I try to add children elements (Say "Cats" and "Dogs") to my programmatically added element "Animals". Cats and Dogs appear on the same level as Animals, as if WP couldn't manage to find their parent... (Yes, I do 3 after 1...)

An idea of what I can be missing ?

share|improve this question

1 Answer 1

you code may be look like this

    add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
    function ravs_add_menu_parent_class( $items ) {
      foreach ( $items as $item ) {
       print_r($item);//print each menu item an get your parent menu item-id
      }
      $link = array (
            'title'            => 'Cats',
            'menu_item_parent' => id of Animals menu like 1372,
            'ID'               => '',
            'db_id'            => '',
            'url'              => 'www.google.com'
        );
      $items[] = (object) $link;
      return $items;    
    }
share|improve this answer
    
Yes, you're right. And as I set myself "Animals" ID, I don't even need to search for it... My problem is that this code works perfectly for an existing parent element, but not with a created* one... (*with the same kind of code) The item gets created, but shows at the same level than its parent... –  Leo May 24 '13 at 19:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.