How to change an image based on the page title

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wmatos
Forum Newbie
Posts: 1
Joined: Wed Dec 16, 2009 3:35 am

How to change an image based on the page title

Post by wmatos »

Hello,

Hopefully someone will be able to give me a hand. I'm developing my first theme for drupal and got stuck trying to add some php to dynamically change an image in a div according to the page's title. Drupal by default will create a variable called $title which is a string with the page's title. So I want to check: If $title is title1, use image1_png. The code I've created is:

Code: Select all

 
<?php 
  $title = "title1";
  $gd_image = "images/side_gd.png";
  if ($title == "title1") {
    <img src="<?php echo htmlspecialchars($gd_image); ?>"/>
  else { <img src="image/other.png"/>
  }
?>
 
Can someone tell me what am I doing wrong? I'm very new to php and can't see to work out...
Thanks in advance!
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: How to change an image based on the page title

Post by daedalus__ »

what is the function htmlspecialentities() for? did you check the output?

the easiest way is to put the title in a variable, output the variable inside the title tag then use the same variable as the filename of the image or something.
Post Reply