I've been trying to do the least amount of custom code for my theme, but I'm struggling to theme the body field before it is output. What I want to do is place a
<div style="clear:both;"></div>
before the body field to make it drop down below an image field that is shown in the earlier fields. Of course I could place this line in the body text itself (yes this works) but its kind of an ugly solution.
I also tried to create a field specific tpl file (field-field_body.tpl.php and also field-body_filter.tpl.php) but this didn't work, the tpl never got called.
My current solution is also a little ugly, but it works, and is located within the actual node-node_type.tpl.php file, which I already need to customise anyway. The code I have is this:
<div class="content">
<?php print str_replace($node->content['body']['#value'], '', $content); ?>
<div style="clear:both;"></div>
<?php print $node->content['body']['#value']; ?>
</div>
Basically I take the content field, remove the body text from it and print the two resulting strings out with the clear in between.
Can anyone provide a more correct solution with a field tpl file?
Thanks.
Comments
Remove the BODY field
I found a few postings that mentioned actually removing the BODY field from the CCK node, and creating my own which was then themable through the normal methods.
I did this and it solved my problem, I now have a special body type which is easily themable with a tpl.php file.
To remove the BODY field from a CCK node, go to:
Home > Administer > Content Management > Content Types > Your Type
And remove the 'Body field label' text.
And you also get the ability to set the weight
I would also recommend to remove this field
By doing so, you get the ability to master its position in the list of fields by setting a weight that would be available without this.
You had it...
Personally, I think your first solution in the node-nodetype.tpl.php was the correct one, with a minor code tweak. It seems to me that this is the 'proper' way to theme your node type.
However, the change I would have made would be to simply not print your $content (nsead of doing the replace command)...instead, just print your fields individually, the way you want them to be (adding any divs or spans that are necessary). Why exactly would you consider it ugly? To me, that's the proper way to do this.
-Bob Christenson
Owner/Designer, Mustardseed Media, Inc.
MustardseedMedia.com
Using default behvior
I guess I prefer to use as much default behavior as possible. And by removing the $content and inserting all the fields I want to see, this would have been quite a lot of code. And then if I want to do something similar to another node type, I would need to do it all over again (ok, I could consolidate it into a function and reuse).
By removing the BODY and creating a custom BODY field, I am able to keep all the existing behavior, and get my desired result in just two lines of PHP code.
Having said that, I think the best solution would have been to provide a tpl file for the original BODY field, but this turned out not to be possible.