If you want more control, you can lay each field out and give it whatever format you want. For example our first_name field, which does not have multiple values enabled, would be displayed as follows (the [0] means it is the first available value for that field):

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

Multiple value fields require a bit of simple php code. You want to scroll through the multiple phone and email fields and print each one surrounded by dd elements. So you code something like this to make sure that all the values get picked up (no need for [0] here because we're going through each field's values one by one):

 <?php foreach ($field_phone as $phone) { ?>
   <dd> <?php print content_format('field_phone', $phone) ?> </dd>
 <?php } ?>

Some fields have more than one formatter available. If you don't specify a format, you will get the default format. For instance, the text module has three formats: 'default', 'plain', and 'trimmed'. The default format uses whatever filter the author selected to display the text. The 'plain' formatter strips all the html tags out of the text, and the 'trimmed' formatter trims the text down to the size specified for a teaser value.

To display alternate formats in your theme instead of the default format, use the following code, substituting the name of your field for 'example' and the formatter you want for 'plain':

 <?php foreach ($field_example as $example) { ?>
   <dd> <?php print content_format('field_example', $example, 'plain' ) ?> </dd>
 <?php } ?>

A logical question at this point is how do you know what fields are available to use. Here is a tip that will help you design your theme. Add the following code to the very end of the template:

 <?php print_r($node); ?>

This will display an array of all the fields and values that are available to you. Do not use this technique in a public area, it will expose information to anonymous users that they should not see! As soon as your template looks good, you should remove that line, but it is a great 'cheat sheet' while you are getting things set up.

Each array key is a field you can use. The name fields will look something like this:

[field_first_name] => Array
   (
     [0] => Array
       (
         [value] => Minnie
         [view] => Minnie
       )
   )

[field_last_name] => Array
   (
     [0] => Array
       (
         [value] => Mouse
         [view] => Mouse
        )
  )

If there are multiple values, you will also see similar elements with the number 1 or 2 or 3. Each of them has a 'value' and a 'view'. In this example, they are the same, but sometimes there will be differences. The 'value' is the raw value for that item that is stored in the database. The 'view' is the formatted 'default' display of that item. Some CCK fields use something other than 'value', like 'nid', to store their values, and for others 'value' is an array of information. Fortunately, you don't need to know what those parameters are, you can use the content_format function to display the correct output for any field.

If you use the Views module, all available formatters will be displayed as options when you add fields to your view, so that is one way to see what formats are available for your fields.

Comments

dman’s picture

content_format('field_example', $example, 'plain' ) 

was working sometimes, and sometimes giving the unhelpful result 'n/a'

It seems that if the current user (eg anonymous) does not have access to the input format originally used for the field (eg full HTML) they can't even SEE the full html entered by the admin that did.
This is a side-effect of check_markup() being careful for some reason.

I fixed it in my case by passing the 4th parameter

content_format('field_example', $example, 'plain', $node ); 

and the content was displayed as required.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

massimoi’s picture

Great suggestion!

Just a little improvement:
if you have PHP in your CCK field, you should use 'default' instead of 'plain'
ie:
print content_format('field_sottotitolo',$node->field_sottotitolo[0], 'default', $node);

warplesnark’s picture

Short comment: displaying the label of a select field is as simple as

print content_format('field_my_dropdown', $field_my_dropdown[0])

Cactii’s picture

I looked around for this piece of code everywhere and since I am new to Drupal and CCK it took me forever to come up with it. Hope it helps someone else.

This pulls the values out of the multidimensional array that CCK makes and puts the data into a simple KEY:VALUE array so that you can easily use it with other code.

*** Note that this only checks to see that the array is EMPTY and not NULL. If you used CCK to create a field and you specify a MAX on how many values can be in that field - the array will be populated with NULL values. If you leave it at Unlimited the array will only have as many values as are filled by the data.

 if (!empty($field_example)) {
	foreach ($field_example as $example) {
		$vtmp = content_format('field_example', $example);
		$array[] = $vtmp;
	}
}

This is handy if you create a content type and have hidden CCK fields to store data which you can process when a user calls on that content type.

Anonymous’s picture

This is the snippet from my tpl for a textarea cck field. I was having trouble in getting the textarea label, however....

<div id="kb" class="field-label"><h3>Your Desired Field Name</h3></div>
 
<div id="kb" class="field-items">
      <?php foreach ($fieldName as $item) { ?>
           <?php print content_format('fieldName', $item, 'default' ) ?>
      <?php } ?>
</div>