1

I want to display an image only on front page through page.tpl.php... However i am not sure about the syntax or where i am going wrong as the code that i type in leads me to en error (syntax error)...here's the code:

<?php  if (drupal_is_front_page())
    {
        <div id ="join">
           <a href="/node/38"><img src="/sites/all/themes/ypwb/joinb.png"></a>
        </div>
    }
?>
  • 3
    You need ?> after the opening parenthesis and <?php before the closing parenthesis. This is so-called embedding HTML in a PHP page. – Sithu Jan 30 '13 at 5:09
  • @Sithu you can add that as an answer. – Mathankumar Jan 30 '13 at 5:30
4

You need ?> after the opening parenthesis and <?php before the closing parenthesis. This is so-called embedding HTML among PHP coding.

Most likely, you can use parenthesis, but you can see the default theme Bartik uses the alternative syntax for control structures (:).

<?php if (drupal_is_front_page()): ?>
    <div id ="join">
       <a href="<?php echo drupal_get_path_alias('node/38'); ?>">
         <img src="<?php echo drupal_get_path('theme', 'ypwb').'/joinb.png'; ?>">
       </a>
    </div>
<?php endif; ?>

You could use page--front.tpl.php as subhojit777 suggested, whereas you are right to use drupal_is_front_page().

As a side note, you should use drupal_get_path_alias() for your node path and drupal_get_path() for your image path.

| improve this answer | |
  • Extra points if using l() and theme_image() :) – David Thomas Jan 30 '13 at 7:49
  • @DavidThomas, you are right. – Sithu Jan 30 '13 at 7:54
5

Rather than editing in page.tpl.php which applies on all pages, you should create a template file called page--front.tpl.php and add image there if you want to display the image only in front page.

EDIT

For more theme hook suggestions refer this page

| improve this answer | |
  • 1
    OP is right to use drupal_is_front_page(). This is a syntax error only. – Sithu Jan 30 '13 at 5:12
  • 1
    kewal is asking to add image ONLY on front page.. may be he is doing wrong in syntax, but I like best practices in Drupal and the template hook I suggested is an example of that :) – subhojit777 Jan 30 '13 at 5:17
  • Yes, the question title sounds like what you said. But if you read the question body carefully, you will see what the actual problem he is facing. – Sithu Jan 30 '13 at 5:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.