I created file node--my_content_type.tpl.php, where I'd like to print the username as string. I tried the following code, but unfortunately it doesn't work. How can I do it?
<?php
print $name;
?>
$node->name
will get you the name of the author who submitted the node. It will return it as string, not as link to the user account.
Usernames are stored in the $user
object.
global $user;
print_r($user);
If you mean the fields in a node, those would be in the node object ($node
).
I highly recommend looking at the output of the above. Get familiar with both the user object and the node object and everything will make a lot more sense.
node.tpl
paste and rename as node--your_content_type.tpl
. The username of node author should be available as $name
.
$user = user_load($node->uid);
print $user->name;
This way you can get the name of author by loading the user.