I use the following code in my page.tpl.php of a Drupal 7 installation:
if (!path_is_admin(current_path())) {
$pathArray = explode('/', current_path());
if (!empty($pathArray)) {
$path_to_node = url("node/".$pathArray[1]);
$menuChildArray = explode('/', $path_to_node);
$menuParent = $menuChildArray[2];
}
}
But on some pages in the admin interface, I get:
Notice: Undefined offset: 2 in include() (line 36 of /home/www/doc/7622/s-d-d.de/testkc/sites/all/themes/sdd2015/page.tpl.php).
The code should only be executed if I am on the frontend...? Why does it get fired in the backend?
At a guess:
The message is in fact generated when you visit a page on the front end, but because you have this code in page.tpl.php (where it shouldn't be), it can't be printed in the messages area until the next page view, as the messages for the current page having already been printed in the same file.
So your code is probably working as expected, but you're seeing a delay on the notice messages being output.
As always, the solution is to check your vars before using them.
// Or whatever conditional makes sense to what you're trying to do.
if (!empty($menuChildArray[2])) {
$menuParent = $menuChildArray[2];
}