Theming the Node Teaser

Last modified: August 23, 2009 - 15:31

You can give a teaser its own unique look by using the following condition on any node.tpl.php file:

<?php
if ($teaser) {
 
// node is being displayed as a teaser
  // Anything here will show up when the teaser of the post is viewed in your taxonomies or front page
} else {
 
//all other cases
  //Anything here will show up when viewing your post at any other time, e.g. previews
}
?>

Or, if you want to use HTML within the conditions, for example to make two completely different layout within the template:

  <?php if ($teaser): ?>
    <!-- teaser template HTML here -->
  <?php else: ?>
    <!-- regular node view template HTML here --> 
  <?php endif; ?>

$teaser vs $page

Note that the $page variable indicates whether a node is standing alone or not, which isn't necessarily the same as whether it's being shown as a teaser. There are legitimate situations where $page == 0 and it's not supposed to be a teaser - for example, when you preview a node. Additionally, $page and $teaser can be set directly by any code that displays nodes via the node_view() function, so it's wise to think about unexpected combinations.

If you wish to provide a third layout for when the node is being displayed as neither a teaser nor a page, try this:

<?php
if ($teaser) { //if node is being displayed as a teaser
  //Anything here will show up when the teaser of the post is viewed in your taxonomies or front page
} elseif ($page) { //if node is being displayed as a full node
  //Anything here will show up when viewing only your post
} else { //all other cases
  //Anything here will show up when viewing your post at any other time
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.