Last updated 14 August 2010. Created on 14 August 2010.
Edited by danielhonrade. Log in to edit this page.

Probably, to create image headers, you are used to creating multiple blocks with images and assigning the visibility only on specific paths.

There's another easier way, which is by creating a single block with php filter turned on and configuring it like below.The benefit of this is obvious, you have only 1 block to create and it's easy to add categories as adding images unlike if you have multiple blocks, because what if you have hundreds of categories, you don't want to create hundred blocks, right?

Step 1

Let say you have the following categories in your blog (created in taxonomy to appear in blog posts):

  • Pets
  • Technology
  • Family
  • Drupal

Step 2

With pathauto installed, set the path to your blog like this, blog/[term-raw]/[title-raw], your URIs should look like this when you create blogs:

  • /blog/pets/my-pet-anna-found
  • /blog/technology/latest-cloud-server-providers
  • /blog/family/vacation-paris
  • /blog/drupal/ultimate-must-have-drupal-module

Step 3

Put your images in a folder (ex. '/sites/default/files/headers/') and name them like this:

  • pets_header.jpg
  • technology_header.jpg
  • family_header.jpg
  • drupal_header

Step 4

Put this code on a block (which will become your content header) with php filter turned on

<?php
// this will get the path like /blog/pets/my-pet-anna-found
$termp = $_SERVER['REQUEST_URI'];  

// this will convert the path to an array 
// like array('blog','pets','my-pet-anna-found')   
$categ = explode('/',$termp);   

// get the array in reverse so that you will always get the second term 
// as the category like array('my-pet-anna-found','pets','blog')             
$categrev = array_reverse($categ);        

// you can assign the second term in the array to another variable
$categ1 = $categrev[1];                     

// add a conditional statement to check if the image file exists or not,
// if it exists then it will load the image   
(file_exists('/sites/default/files/headers/' . $categ1 . '_header.jpg')) ? print '<img src="/sites/default/files/headers/' . $categ1 . '_header.jpg" alt="' . $categ1 . '"/>': '';  
?>

Now, if you have multiple terms in hierarchy and you want to show each term with image header, you can just manipulate the term array and conditional statement

Great, isn't it?

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

nevets’s picture

You can generalize this using the taxonomy image module and letting it handle the term to image mapping.

danielhonrade’s picture

You're right, anyway this is just to show a simple code to solve a simple problem of loading images associated with paths.

Understanding this code will also lead to solving other simple problems related to paths such as theming menus,
theming blocks or page differently depending on paths, etc.

Daniel Honrade, Jr.

Freelance
email: mail@danielhonrade.com