Community & Support

Different Header Images for Different Nodes

Drupal provides same header for the whole site and if we want to theme all the nodes individually then it’s a bit complicated process. While looking for alternatives to this, the following snippet work fine, this has been implemented in www.vinrcorp.com you can see the example before using it. The usage of the snippet is also quit simple just put this code into the header part or where ever you want, of page.tpl.php file of your theme.

<table>
  <tr>
    <td><img src="[path to image folder]/images/<?php
   
if ($node->nid == 1) {
    print
'image-1.jpg" />';
    }elseif (
$node->nid == 2){
    print
'image-2.jpg" />';
    }elseif (
$node->nid == 3){
    print
'image-3.jpg" />';
    }elseif (
$node->nid == 4){
    print
'image-4.jpg" />';
    }elseif (
$node->nid == 5){
    print
'image-5.jpg" />';
    }elseif (
$node->nid == 6){
    print
'image-6.jpg" />';
    }elseif (
$node->nid == 7){
    print
'image-7.jpg" />';
    }elseif (
$node->nid == 8) {
    print
'image-8.jpg" />';
    }elseif ((
$node->nid == 9) | ($node->nid == 10)){
    print
'image-9.jpg" />';
    }elseif (
$node->nid == 11) {
    print
'image-11.jpg" />';
    }else {
    print
'default-image.jpg" />';
    }
?>

</td>
  </tr>
</table>

To use it just put images in the image folder and mention the path in the code and also replace the names of images as per the node ids. This will help you to display different banner images for different nodes.
I think you can use it and this works for you too. You can reply to this post for further help.

Vivek Dubey

Comments

How to assign different header image for each taxonomy/category

Nice one, I think your post should be placed under Handbookes/PHPTemplate Theme Snippets
http://drupal.org/node/45471

and the next question would be;
How to assign a header image for each taxonomy/category instead of each node as your example above...
any definitive snippet on this?

Take a look at the taxonomy image module

Take a look at the taxonomy image module which allows you to associate an image with a taxonomy term. It provides a function to get the image (as an image tag) associated with a given term (see the README).

Taxonomy Image

Taxonomy Image is able to place a taxonomy-based image in a node, but I haven't figured out how to place the images in page.tpl.php (e.g., a banner).

If anyone knows how to put taxonomy images in page.tpl.php, I'm looking for the answer...

Thanks

hi Vivek. Your snipplet is

hi Vivek.
Your snipplet is great. It works fine.
I was wondering how could I change the header in pages like contact that doesn´t have an ID. Would you know?
Thanks.
Michaël

Michaël

Hi Michaël, Your problem

Hi Michaël,
Your problem can be solved by using the following snippet;

}elseif (arg(0) == 'contact') {
print 'your image name goes here" />';

I hope you got the idea that in elseif you are required to check if arg(0) == 'contact'
And if true display image of your choice.
I hope this should work as I have tested it, but in case you face any other problem let me know.
Best of luck.

Vivek Dubey

www.vinrcorp.com
Motivates Core Competence

www.vinrcorp.com
Motivates Core Competence

Hi Vivek, Thanks so

Hi Vivek,
Thanks so much.
Michaël

Michaël

Hi!

First great script, thnx!
But I'm still keep getting a error message when I double click on the contact link??

This is the error:
"Notice: Undefined variable: node in include() (line"

This is the code I've used:

<div class="
<?php
       
if ($node->nid == 7 || arg(0) == 'home') { print 'homeClass'; }   //Home
       
else if (arg(0) == 'contact') { print 'contactClass'; }                      //Contact page
       
else { print 'allClass'; }                                                                 //All other pages/nodes
?>
">
</div>

Please help :(

[Edited to add <code> and </code> tags: nevets]

You do not say where you

You do not say where you placed the script, but $node is not available by default, so the error suggests $node->nid == 7 should read (!empty($node) && $node->nid == 7)

It works (but there error)

@Nevets, thanks! ... BUT... I still have the error in the admin section

"Notice: Undefined variable: node in include() (line"
"Notice: Trying to get property of non-object in include() (line"

I've placed the code in the header of my theme 'page.tpl.php'.
This are the two solution (I thought) worked for me;
Solution 1

<div class="
<?php
       
if (arg(0) == 'home') { print 'homeClass'; }  //Home
       
else if (arg(0) == 'contact') { print 'contactClass'; }  //Contact page
       
else { print 'allClass'; }  //All other pages/nodes
?>
">
</div>

Solution 2

<div class="
<?php
       
if (!empty($node) && $node->nid == 7) { print 'homeClass'; }  //Home
       
else if (arg(0) == 'contact') { print 'contactClass'; }  //Contact page
       
else { print 'allClass'; }  //All other pages/nodes
?>
">
</div>

Maybe not the right way ... but it works for me ;)

I've found a fix/hack....
Maybe it's not the right way to do this but I found a way to hide the errors.

Just place this code "error_reporting (E_ALL ^ E_NOTICE);" above the first if statement.

<div class="
<?php
        error_reporting
(E_ALL ^ E_NOTICE);
        if (..........
?>
">
</div>

Hi Can't we use one cck image

Hi

Can't we use one cck image field associated with nodes and then upload image whatever we want. Then using theme_preprocess_page() function we can achieve same thing.

Thanks

regards
Dev/Kuldev

I've got no experience with that..

Hi Devtherock,

I don't know, I don't have any experience with your sulotion :)

nobody click here