Created
February 10, 2012 00:02
-
-
Save oliverguenther/1784525 to your computer and use it in GitHub Desktop.
Drupal 7 Snippet: Select a header image based on the topmost active menu item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return header image based on the node id of the topmost active menu item | |
* @return Absolute path to the image selected | |
*/ | |
function get_header_image() { | |
// Get the Page's Parent Menu Item | |
$menuParent = menu_get_active_trail(); | |
// background images based on node id's | |
$node_imgs = array( | |
8 => 'header1.jpg', | |
12 => 'header2.jpg', | |
25 => 'header3.jpg', | |
); | |
$header = 'default_header.jpg'; | |
// Get the topmost menu element | |
if (!empty($menuParent[1])) { | |
$linkpath = $menuParent[1]['link_path']; | |
if (!empty($linkpath)) { | |
// search for node id in it's link_path | |
preg_match("/node\/(\d+)/i", $linkpath, $match); | |
if (!empty($match) && !empty($node_imgs[$match[1]])) | |
$header = $node_imgs[$match[1]]; | |
} | |
} | |
// return absolute path to the | |
return base_path() . drupal_get_path('theme', 'MY_THEME_NAME') . '/images/header/' . $image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment