0

I'm trying to display some custom user fields of an author, of a content type's nodes. The only fields in $node that relate to the author seem to be $node->name and $node->uid. I tried this in node.tpl:

global $user;
print render($user->field_foo);

and it makes the field show up in Krumo, but it comes up as

field_foo (NULL)

When I get more specific, using what's available to me in the user page, $field_foo['und'][0] in this case, I'm given something similar:

field_foo (Array, 1 element)

und (Array, 1 element)

0 (NULL)

I'm thinking I need to target the UID or name somehow, but I don't know how. What's described in the answer here sounds like what I want, but I'm not sure how to do what they're describing. It sounds like I should maybe be using a node_submit function. Something like this maybe?

function MYMODULE_node_submit($node) {
  if (isset($node->name)) {
    if ($account = user_load_by_name($node->name)) {
      $node->uid = $account->uid;
    }
    else {
      $node->uid = 0;
    }
  }

  $node->uid->field_foo;

  return $node;
}
1

I think if you need show more info of author of node, you can try this way.

STEP 1

in file template.php write hook_preprocess_node

function YOURTHEME_preprocess_node(&$vars) {
  $node = $vars['node'];
  $user = user_load($node->uid);

  // if you want render field user
  $phone_render = field_view_field('user', $user, 'field_phone')
  $phone_html = drupal_render($phone_render);

  // if you want get value of filed
  $phone = field_get_items('user', $user, 'field_phone');
  $phone_value = $phone[0]['value']; 

  $vars['phone_value'] = $phone_value;
  $vars['phone_html'] = $phone_html;
}

use need change field_phone with your machine name of your field user.

STEP 2 In file node.tpl.php. You can use

<?php
  print $phone_value;
  print $phone_html;
?>

print where you need.

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.