I’ve been building custom content types with the CCK module and needed to create a custom node template file.
It’s pretty easy, but I had a hard time finding tutorials so hopefully this will help someone get started:
1) Take your node.tpl.php file and save it as node-your_content_type_name.tpl.php. If you don’t have a node.tpl.php file you need to create one and place it in your theme directory or Drupal won’t detect the content type specific template. What I did was copy the node template from the Garland theme and then thin it down some.
2) You need to empty your site Cache, go to Site Config –> Performance and dump the cache, then visit the Site Building -> Themes page to get Drupal to read your theme files. This makes sure that it knows the new template files are there. I’ve had kind of a mixed bag with this, sometimes I’ve had to do it, sometimes not, but if you node template isn’t being applied, give this a try.
3) Now, we need to figure out what fields to call and how to call them into your node template. You’re also probably going to want to wrap them in some div columns or whatever you need to setup the layout the way you want.
to print all of your fields you can use this code:
print all nodes:
<pre style="background:#eee;border:1px solid black;clear:both;"> <?php print_r($node) ?></pre>
Then you can call any of the pieces of the array for each field, like this:
<?php print $node->field_thename[0]['view'] ?>
- Print tells it to print what follows
- then we call the cck field that we want
- the [0] specifies the item in the array, if you have multiple entries, such as images all attached to the same imagefield you could call the 2nd with [1] or the third with [2] and so on.
- the last piece I believe is called the field particle… “view” seems to work for me to render the content like it should be. Pretty much like how it puts the content in just a normal template. Some other “particles” to try are: ['safe'] ['value']
But you can also call other “particles” of data that is in the array. Look below at how I build an image tag for example by calling each piece.
call path to image and build img tag:
<img src="<?php print base_path() . $field_banner[0]['filepath']; ?>"
width="<?php print $field_banner[0]['data']['width']; ?>"
height="<?php print $field_banner[0]['data']['height']; ?>"
alt="<?php print $field_banner[0]['data']['alt']; ?>"
I hope this helps someone building a Drupal theme.
Acketon is located in Hammond, Louisiana on the Northshore.