1

I have a content type that uses many cck fields, and I would like to theme the content type by grouping certain fields in "blocks" (not Drupal blocks but div blocks), preferably div tags, so that the fields aren't just displayed one after the other. I can edit the css of individual fields, but if I want a field to be in a div on the top right hand corner of my node types page, and then another group of fields to be along side it, and then another group of fields to fall underneath all of the other fields, that css could get messy and interfere with my themes main css. I'm not sure what to put in my template file, since all the fields just fall under "content."

I'm using Drupal 6, and a little new with php, but can do basic "print" and "if" conditions. Any help would be greatly appreciated! Thanks in advance!

1

In your node templates, you have access to the $content variable, which includes all of your fields mushed together as CCK et al wants them, but you also have the $node object which includes all the raw information about your node.

So, first thing to do is to put a

<php drupal_set_message('<pre>' . print_r($node, TRUE) . '</pre>'); ?>

at the beginning of your node-TYPE.tpl.php file to see what you have to work with fieldwise. There you will see lots of information, but basically, it is broken down into stuff like

 field_name[0]['value'] = // raw information stored in the database
 field_name[0]['safe'] =  // the above info in a safe format
 field_name[0]['view'] =  // the above info passed passed through the widget

So. If you want to put some of the fields in one <div> and some in another you can do something like

 <div class="whatever">
   <?php print $node->field_name[0]['view']; ?>
   <?php print $node->another_one[0]['view']; ?>
 </div>
 <div class="another">
   <?php print $node->yet_another[0]['view']; ?>
   <?php print $node->one_more[0]['view']; ?>
 </div>

Now, of course, you probably don't want to create everything yourself, so you then add

 <?php print $content; ?>

but lo and behold, you get the fields you printed manually in there as well. To get rid of that duplication, go into your content edit screens and in the Display Page (admin/content/node-type/NODETYPE/display) just exclude them from $content in your teaser and/or full node display) and they should be displayed just once now in your custom <div>s.

When all is working, take out that drupal_set_message() so you don't totally confuse your users :)

  • glad I could help out! – Jimajamma Sep 1 '12 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.