开发者

Theming node body in Drupal 6

I'm trying to make Drupal output the body of a particular content type in div tag with certain class name.

First, I tried to override node.tpl.php, but I discovered $content variable here already contains bo开发者_运维知识库dy along with additional custom cck fields. And what I'm trying to do is precisely to separate body from another fields.

After that I looked at content-field.tpl.php and found it's executed only for custom cck fields. So doesn't suit me too.

So.. how to theme a node body field?


Start by calling

print_r($node);

This is what you node object contains - and you can use any of its members directly instead of just printing the content. Analyse this and you'll know what to call exactly ;)

In case of the body, the unstyled content can be accessed with:

$node->content['body']['#value']

For other fields, you do:

$node->field_name[0]['view']

(where [0] is the index of the item in the array - useful for ImageField when you can upload many pictures).

For example, here's the content of my node-event.tpl.php, displaying details about an event:

<div class="event clear-block">
<?php
    $class = (convert_datetime($node->field_event_date[0]['value']) < time()) ? 'past' : 'future';
    echo "<h3 class='header'>When?</h3><p class='$class'>".$node->field_event_date[0]['view']."</p>";
    echo "<h3 class='header'>Where?</h3><p>".$node->field_event_place[0]['view']."</p>";
    echo "<h3 class='header'>What?</h3>".$node->content['body']['#value'];
    echo "<h3 class='header'>How much?</h3><p>".$node->field_event_price[0]['view']."</p>";
    echo "<h3 class='header'>How to participate?</h3>".$node->field_event_subscribe[0]['view'];
?>
</div>


Take a look at this: http://capellic.com/blog/theming-the-node-body-field


If you need to be very flexible, the best way for me is to go to this page :

admin/content/node-type/[ContentType]/display/basic

And set all your additional fields to "hidden".

Then, the $content variable will contain only the body field. The drawback is that you need to display all the additional fields one by one. CCK provide an easy way to do that by providing a fully rendered variable for each field

print $field_FIELDNAME_rendered;

Using that, you can really customize easily the output for a content type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜