getstarted
getstarted

Reputation: 1

How to display parent menu item in node template? Drupal 7

How do i display the parent menu item in the node template?

I want to display parent menu item along with current page; but i dont need others.

Edit: I enabled menu breadcrumb module and added the following code:

<?php              
                $menuParent = menu_get_active_trail();
                if (sizeof ($menuParent) >= 2 && $menuParent[2]) {
                     $menuParent = $menuParent[1]['link_title'];
                     print $menuParent; 
                } 
            ?>

It is working fine, but I am getting an error for the pages which doesn't have 2nd level navigation: Error: Notice: Undefined offset: 2 in include()

I thought my condition sizeof will take care of the problem, but not working.

Upvotes: 0

Views: 8130

Answers (2)

Duncanmoo
Duncanmoo

Reputation: 4221

Use PHP array tools to get you the right item in the array:

<?php
  $menuParent = menu_get_active_trail();
  //get rid of the last item in the array as it is the current page
  $menuParentPop = array_pop($menuParent);
  //Just grab the last item in the array now
  $menuParent = end($menuParent);
  //if it is not the home page and it is not an empty array
  if(!empty($menuParent) && $menuParent['link_path'] != ''){
    print $menuParent['title'];
  } else{
    print $title;
  }
?>

Upvotes: 8

Sarah Vessels
Sarah Vessels

Reputation: 31670

You're checking $menuParent[2] but then using $menuParent[1]. Maybe check $menuParent[1]:

if (sizeof ($menuParent) >= 2 && $menuParent[1]) {

PHP's arrays are zero-indexed, so slot 1 is the second slot.

Upvotes: 0

Related Questions