0

I created a custom field in the users profiles.

I need to display in node.tpl.php, this field for the author of the node.

I tried :

<?php 
print $node_author = user_load($node->uid); 
print ($node_author->field_custom); 
?>

But i have an error :

Recoverable fatal error : Object of class stdClass could not be converted to string

Do you know where I'm wrong? And, is it the best way of proceeding?

2
  • 1
    print $node_author = user_load($node->uid); in this use nid $node is represnted in nid or use $profile
    – Naveen
    Commented Mar 10, 2016 at 12:10
  • 1
    $node->uid = $user->uid;
    – Naveen
    Commented Mar 10, 2016 at 12:11

4 Answers 4

0

Replace the above code with below one:

<?php 
  $node_author = user_load($node->uid); 
  print ($node_author->field_custom); 
?>
1
  • It does not work.Error : Notice : Array to string conversion in include()...
    – Fluidouix
    Commented Mar 11, 2016 at 14:05
0

You are using print when assigning into a variable, that's you are getting error.

remove print from line 2 and the code should be as below

<?php 
$node_author = user_load($node->uid); 
print render($node_author->field_custom); 
?>
3
  • It does not work.Error : Notice : Array to string conversion in include()...
    – Fluidouix
    Commented Mar 11, 2016 at 14:05
  • Ca`n you please provide what print_r($node_author); output.
    – NTT
    Commented Mar 11, 2016 at 14:27
  • a big line wich i cut a little and who start with : stdClass Object ( [uid] => 3 [name] => name [pass] => (...) [mail] => [email protected] [theme] => [signature] => [signature_format] => filtered_html [created] => (...) [access] => (...) [login] => (...) [status] => 1 [timezone] => Europe [language] => fr [picture] => [init] => [email protected] [data] => Array ( [ckeditor_default] => t [ckeditor_show_toggle] => t [ckeditor_width] => 100% [ckeditor_lang] => en [ckeditor_auto_lang] => t ) [roles] => Array ( [2] => authenticated user [4] => (....)
    – Fluidouix
    Commented Mar 11, 2016 at 14:35
0

Try with print render($node_author->field_custom);

Or you can try as well:

insert at the top of you node.tpl.php

<?php 
global $user;
$node_author = user_load($user->uid);
$yourvariable = $node_author->field_custom;
?>

Where you want print the field insert:

<?php print $yourvariable['und'][0]['value'];?>
4
  • It does not work. I have an error: Recoverable fatal error : Object of class stdClass
    – Fluidouix
    Commented Mar 10, 2016 at 17:36
  • Mmmm...try: print $node_author->field_custom['und'][0]['value']
    – kb8
    Commented Mar 10, 2016 at 23:31
  • Does'nt work too ;) error : Notice : Undefined index: und dans include()
    – Fluidouix
    Commented Mar 11, 2016 at 14:07
  • @Fluidouix I have update my answer. Try.
    – kb8
    Commented Mar 11, 2016 at 17:13
0

Issue is in the way you are printing the value. actually print is just used for printing string, while print_r is used for printing array or Object. so in your case when you use $node_author->field_custom, then as it is an array, therefore it is not printing and giving you error. You can print it as below.

print_r($node_author->field_custom);

It will print the array, and will not give you error, however if you just want to print the value, then you can print somewhat like:

print $node_author->field_custom[LANGUAGE_NONE][0]['value'];

If above code doesnot work, then paste output of

print_r($node_author->field_custom);

It will give better approach.

//Edited

$image_url = file_create_url($node_author->field_custom[und][0]['uri']);
print render(theme_image($image_url));

Or

print render($node_author->field_custom);
3
  • Thanks Manoj for this detailed answer, I tried your various solutions, but all return me an error, Recoverable fatal error or Notice : Undefined index: value in include()
    – Fluidouix
    Commented Mar 11, 2016 at 14:25
  • dont use print $node_author->field_custom[LANGUAGE_NONE][0]['value']; Just use print_r($node_author->field_custom);, after that I will be able to give the answer. Commented Mar 11, 2016 at 14:29
  • It returns me : Array ( [und] => Array ( [0] => Array ( [fid] => 17 [uid] => 1 [filename] => name.jpg [uri] => public://name.jpg [filemime] => image/jpeg [filesize] => (...) [status] => 1 [timestamp] => (...) [rdf_mapping] => Array ( ) [alt] => [title] => [width] => 199 [height] => 300 ) ) )
    – Fluidouix
    Commented Mar 11, 2016 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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