I have a Drupal site that needs to display a unique header image based on the path. I have found some helpful code. It gets me close to where I need to be, but not all the way. I have pasted it at the end of this post.
The issue I am having is that it bases the banner image off of the characters after the first “/” after example.com in the URL. For example, example.com/forum returns a banner of header-FORUM.png.
I need it to work a little differently. I would like it to base the banner returned off the characters after the second “/” after example.com in the URL. For example, example.com/category/term should return a banner of header-TERM.png.
Any help that you can offer with this is much appreciated.
Here’s the code I mentioned earlier via AdaptiveThemes (FYI, there is a comment on that page that attempts to solve a similar issue to mine but I can’t get it to work).
<?php
// Return a file based on the URL alias, else return a default file
function unique_section_header() {
$path = drupal_get_path_alias($_GET['q']);
list($sections, ) = explode('/', $path, 2);
$section = safe_string($sections);
$filepath = path_to_theme() . '/images/sections/header-' . $section .'.png';
if (file_exists($filepath)) {
$output = $filepath;
}
else {
$output = path_to_theme() . '/images/sections/header-default.png';
}
return $output;
}
//Make a string safe
function safe_string($string) {
$string = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '-', $string));
return $string;
}
?>
Thanks!
Not exactly sure what the output of
drupal_get_path_aliasis, but try this:The only changes made were to the usage of
explode.explodewill separate the path based on the/, so you just need to access a different element in that array. The last parameter ofexplodeis the maximum number of elements to be returned and may also need to be tweaked