Theming the node “Body” field

I’ve always struggled to theme the Body field of a node. Sure, there is a “content” wrapper, but you lose the ability to reference the field through CSS once you start adding CCK fields. I finally took the time to find a solution.

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:

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?

Download the module >