Theming the node "Body" field

By Stephen Musgrave
Jul 27, 2009
If you've ever tried to theme the "Body" field after adding at least one CCK field, then you know my pain – there is no way to reference the body field through CSS – there is no element to hook into.

This morning, I went out looking for a solution and found that someone had contributed a module in the Drupal issue queue.  I downloaded the module and investigated the contents and – of course – it's so simple that I'm felt a bit sheepish for not having discovered this solution earlier.  The module simply wraps a <div class"node-body"></div> around the body field.

There were some problems with the module code in the ZIP file that was initially uploaded as it didn't declare a Drupal version and there were some syntax issues in the module file.  After a minute of crossing t's and dotting i's, I came up with this:

<?php
function bodywrapper_nodeapi(&$node, $op, $teaser, $page) {
  if ($op == 'view') {
    $node->content['body']['#value'] = "<div class=\"node-body\">" . 
      $node->content['body']['#value'] . "</div>";
  }
}

Simple, isn't it?

Comments

Nice find. It's funny what a

Nice find. It's funny what a basic, obvious, totally overlooked feature this is in Drupal. I hope they implement it in 7...

With "Fields in Core", I

With "Fields in Core", I would certainly hope that Title and Body are seen as CCK fields. While the DB structure still might have them as old-school node fields, I would hope that they are as feature rich and malleable as CCK fields.

Beautiful

This works perfectly! Thank you.

It's kind of ridiculous considering the otherwise div overkill mantra of Drupal, that this hugely important div would be left out.

Too bad it requires a full custom module. Would be great if there was a way to do this at the theme level.

After hours of searching, it works!

How simple...
But I'm wondering, could this be done in a .tpl.php file or by using a function in the template.php file?

You could, but then you get

You could, but then you get into the problem of having to explicitly echo out all your fields fore each content type you create -- you would need a separate tpl file for each node type -- because the CCK fields and the Body field would all be merged into the content field in the node array. Depending on your themeing, you may want to do that anyway, but this module handles most cases for me.

Doesn't work

So I do. tpl.php file didn't work. oyun indir

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
    Drupal Association Organisation Member     N-TEN Member
X
You may login with either your assigned username or your e-mail address.
The password field is case sensitive.

Loading