1. Creating a template

Last modified: April 30, 2007 - 19:57

Copy the code below (taken from a typical node.tpl.php file) into a text editor and save it into your theme's folder. The name of the file depends on your version of CCK. For 4.7 use node-content_example.tpl.php (substituting the name of your content type for 'content_example') and for 5.x use node-example.tpl.php (again, replacing 'example' with your content type's name).

<div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
    <?php if ($picture) {
      print
$picture;
    }
?>

    <?php if ($page == 0) { ?><h2 class="title"><a href="<?php print $node_url?>"><?php print $title?></a></h2><?php }; ?>
    <span class="submitted"><?php print $submitted?></span>
    <span class="taxonomy"><?php print $terms?></span>
    <div class="content">

      HERE IS WHERE YOUR CONTENT WILL GO

    </div>
    <?php if ($links) { ?><div class="links">&raquo; <?php print $links?></div><?php }; ?>
  </div>

Now navigate to a node using that content type, something like:

http: //www.example.com/node/27

You will see the page headers, links, etc., but will just see the message 'HERE IS WHERE YOUR CONTENT WILL GO' where the content should be because you haven't put anything in there yet.

There are a lot of ways to fill in the content. The simplest and most basic is to just output the node's default content, using the following code:

...
<div class="content">
  <?php echo $body ?>
</div>
...

This will give you a node that has all the default formatting. If you like that presentation, your template is done and you're ready to go.

...<div class="content"> 

chillpenguin - February 11, 2008 - 17:01

...
<div class="content">
  <?php echo $body ?>
</div>
...

^^ I don't believe that is correct. Using this causes no content to appear.

...
<div class="content">
  <?php echo $content ?>
</div>
...

^^ This one works though.

Getting the original body

oxcoda - April 9, 2008 - 06:09

I found the following to be the best way to get the original body out:
<?php echo $node->body; ?>
Otherwise you also get the content with the custom fields added.

The above can be used for any custom (CCK) field, eg:
<?php echo $node->field_first_name; ?>

Note: I am using the dev version on Drupal 6.

I'm using 6.2 - does this

estim8d - May 7, 2008 - 00:20

I'm using 6.2 - does this still work?

<?php
echo $node->field_first_name;
?>

I just get after selecting a field, "array" in text...

oops - just read the next page - use this instead:

<?php
print content_format('field_projectsummary', $field_projectsummary[0]);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.