This is a step by step instruction with code on how to theme your Drupal node-custom.tpl.php files. Theming a Node in Drupal is the number one way to set your Drupal site apart.

If you understand basic HTML, CSS, and PHP then you are already most of the way there, if not, then try to follow what I'm going to say.

I first stumbled across an amazing piece of code by watching a podcast from Mustardseed Media Inc. I'm going to just expand on what he said and go into a little more detail here and there. I would recommend watching a few more of his podcasts as he does a really good job showing you how to do basic Drupal things.

First make a dummy node entry for the type that you will be modifying, we will use it later.

Then make a new node template for the node you are trying to custom theme. This is done in two ways:

  1. Copy the existing node.tpl.php file (if you only want to move a few things around) and rename it to node-custom.tpl.php, the "custom" part is the machine readable name you gave your node.
    For example: a blog entry would be node-blog.tpl.php
  2. The other way is to simply make a new text file on your computer and name it node.tpl.php, this will give you a completely blank canvas to work with.

Once you have your new file made, put this piece of code at the bottom or if you started new then simply paste this code into your node-custom.tpl.php:

<?php print_r($node) ?>

Either just save your file or upload it to your ftp, depending on how you are working on your site.
Refresh your browser on the dummy node entry you made earlier. Notice now that after your content a bunch of other stuff now shows up. That is what we will be referencing later.

To use the code that is displayed on your node entry properly you must understand what it is doing.
These are php functions that we will call to our template and then manipulate to our hearts desire. Lets take a simple piece to start out with. I've set up a demo node with a node-demo.tpl.php you can find HERE. In the node-demo.tpl.php file, all I have printed is

<?php print_r($node) ?>

The text in the [brackets] are the functions that we are going to be using.
Starting off with something simple:

<?php print $node->content ?>

I want you to notice a few things here all the php coding we will be doing will all include:

<?php print ?>

We are simply printing the desired functions. The next part $node is referencing that we are working with a node type. and finally notice that the first level of the function isn't in [brackets]. If you use the rest of the function they will need to be enclosed in [brackets] like this:

<?php print $node->content ['body]['#value']'?>

Make sure you use 'apostrophes' around the text in the [brackets], unless it is a number.

To print the content that you want. First, find the field that you want and then find the appropriate children or layers below that field until you get to the actual text, image, or other content. For example if you look again at the demo page HERE.

If I wanted to print the custom cck text field I made for the node. I first find the [field_custom], that is the machine readable name I made when setting up the cck field for the node. Note that there are actually two of these, you want to find the one that is not under the [content]. Then I find either the array above the final function or the final function itself. In this case I find the array [0]. Then I find the final function [value].

Now you put the php code together like this:

<?php print $node->field_custom[0]['value'] ?>

Now that you can print the functions you want, you can simply re-order them to suite your needs or change the css to fit your needs.

You can customize the style.css file also found in your theme's folder.
For example:
If you want a 1px black solid border around your content you make a new id in your style.css that looks something like this:

#contentbox {
    border: 1px solid #000;
    }

And then on your node-custom.tpl.php put this:

<div id="contentbox">
   anything you want in this div goes here
</div>

Update your online or offline files and see the change.

These are the basics for customizing a node from the php to the css. With this knowledge go out and make great sites that look great, function better, and fit you specific needs not the default needs of all Drupal sites.

Comments

good stuff

good stuff - easy to read and understand - thanks!

In order to accomplish your

In order to accomplish your end goal you must modify Drupal and in the end that means with some piece of custom code somewhere along the way. Whether this is overriding a theme or modifying a module, nine times out of ten, you are going to have to write code.

Where do I "find" CCK stuff?

Great post -- I think between this and your post on argument PHP code you're going to help me solve the problem that had me up until 4am last night. I hope you can help me put it all together...

You say "first find the [field_custom], ...Then I find either the array above the final function " but you don't say *where* to find these things!

I'm trying to build a view using a user reference cck field,

if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
return $node->field_related_user[0][uid];
} else {
return FALSE;
}

I think, maybe, the [uid] field is what I have wrong (since I just guessed at that value name).

Thanks!
M

Think I'm closer...

Sorry to over-comment, but your posts are just *so* close to what I need...

I reread your post, created a tpl file to dump out all the fields and I get

[field_related_user] => Array ( [0] => Array ( [uid] => 6 [view] => Joe )

Which makes me think that the variable I want to get at is

$node->field_related_user[0][uid]

But that's not working... I can see in the SQL

AND (node_data_field_related_user.field_related_user_uid = 0)

Thanks again for any light you can shed... if nothing else my comments might help someone else!

m

What are you trying to argument?

I'm not sure what you are trying to argument. But if it is just the uid then make sure you don't for get the ' ' so it would look like

$node->field_related_user[0]['uid']

but not in the 0 part just the uid part.

would you mind telling me how

would you mind telling me how you nessted comment are maded?thank you!

Threaded Comments

Go to www.yoursite.com/admin/content/node-type/yourcontenttype

There you will find the comment section towards the bottom, expand it and select the "Threaded list - expanded" option.

There are several other comment modules over at drupal.org that you might be interested in as well.

Any way to style different

Thanks for this tutorial it helped me alot,

But is there any way to style a node differently when it appears on the frontpage and when it appears on its own node URL? /nodes/29 e.g. ?

Is it true that Drupal

Is it true that Drupal software is helpful while designing a website?

SEO
http://www.webmarketingexperts.com.au/

So as web marketing experts

So as web marketing experts you thought it would be a good idea to spam my comments so you could have a link back to your site? I'm not sure but that sounds like sketch business practices!

I'll leave your comment up for a while to demonstrate how bad that actually looks.

-Hank

Thank you very much for the

Thank you very much for the excellent and useful subject.

It's more like of sharing

It's more like of sharing reference than spamming with bad intentions.

Not really

I would like to think so but the comment left has nothing to do with the blog at all. It relates to the topic, but not to the blog, this is what makes it spam.

it works well,,thank you very

it works well,,thank you very much

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options