2

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;
?> 
4

$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.

  • Thanks for your answer. Yet, if I coded <?php print $name; ?>, on the page appears term to the author. Doesn't it matter? – user19455 Aug 14 '13 at 7:44
  • 1
    Yes I know that,since you want to display that as a literal string I gave you $node->name. If you want the name with link you can print $name itself. – Ram Aug 14 '13 at 7:47
  • @Ram: thanks i too got it. For me it was coming as links, while printing $name. After i printed $node->name i got as string. cool :) – NewToDrupal Aug 14 '13 at 9:04
  • I've problem with it. In php code I've echo "<div class=\"custom-buy-button\"> <button onclick='buyPhoto(\"<?php print $node->name; ?>\")'>Buy</button> </div>"; and I have function in JS <script> function buyPhoto(user) { alert(user); } </script> Unfortunalety on the sreen appears <?php echo admin; ?> I don't know why in the alert is php tag. – user19455 Aug 14 '13 at 9:13
  • @user19455: what problem ? – Ram Aug 14 '13 at 9:15
0

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.

  • Firstly, I copied to my file node.tpl.php file. Now I want to write user who own a given content. – user19455 Aug 13 '13 at 21:19
  • Could you give me some directions. Unfortunately, I don't know what to do. When I want to write output print $node. There are errors. – user19455 Aug 13 '13 at 21:29
  • copy node.tpl paste and rename as node--your_content_type.tpl. The username of node author should be available as $name. – Jack-PL Aug 13 '13 at 23:00
  • I tried it. I want to have string so I've coded "<?php print $name; ?>", yet it doesn't work. Maybe, for that $name is a term. – user19455 Aug 14 '13 at 7:11
  • I removed the part answering about the node field, since there is another question asking about that. – kiamlaluno Aug 14 '13 at 16:05
-1
$user = user_load($node->uid);
print $user->name; 

This way you can get the name of author by loading the user.

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.