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>
    }
?>
2
  • 3
    You need ?> after the opening parenthesis and <?php before the closing parenthesis. This is so-called embedding HTML in a PHP page.
    – Sithu
    Commented Jan 30, 2013 at 5:09
  • @Sithu you can add that as an answer. Commented Jan 30, 2013 at 5:30

2 Answers 2

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.

2
  • Extra points if using l() and theme_image() :) Commented Jan 30, 2013 at 7:49
  • @DavidThomas, you are right.
    – Sithu
    Commented Jan 30, 2013 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

3
  • 1
    OP is right to use drupal_is_front_page(). This is a syntax error only.
    – Sithu
    Commented Jan 30, 2013 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 :) Commented Jan 30, 2013 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
    Commented Jan 30, 2013 at 5:21

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