Hi ladies & gentlemen,

I'm trying to customize a node--TYPE.tpl.php template, and I am wondering how I can print out a custom user profile field in it.

Basically, I have a user profile field called 'field_occupation', where a user enters his/her occupation. Then inside this content type, that is submitted by users, I want a line that prints out the author's username and his/her occupation.

I googled and searched Drupal.org for some answers, but I only found code snippets for Drupal 6 and they don't seem to work for me (in Drupal 7).

I hope someone can help me out here, that would be very kind. Thanks!

Comments

jamiegotluckies’s picture

I hate to do this, but, Bump!

markfinney’s picture

If you use the function: user_load($user->uid) then print_r the resulting object you will see how to access the info you need.

http://api.drupal.org/api/drupal/modules--user--user.module/function/user_load/7 for a better example!! (Read the comments...)

Hope that is helpful.

Mark

jamiegotluckies’s picture

Thanks, Mark, for the reply.
See, the thing is, that still only prints the user information of the user currently viewing or logged in the site. What I want to simply do is, say I'm visiting the profile page of Jane, I want to include some information that is going to say, for example, "About Jane". While I do understand that something like,
<?php print render($user_profile); ?>
in user-profile.tpl.php prints out Jane's profile picture, I just can't seem to figure out how to simply print her username. I almost feel a little dumb at how I can't seem to execute such a simple function.

SharonD214@aol.com’s picture

I'm trying to do something very similar and can't figure it out either.

Sharon

jjones2008’s picture

I am trying to figure out the same thing. I cannot believe that there is no info about this.

KingOfMyCastle’s picture

Hi, I think I have a similar problem to you. I've created a new field called "field_job_title" for the native "People/User" content type.

I'd love to get hold of this field to use in my Blog Post content type node, "node--blog.tpl.php".

I can already print out the username by "print $name;". I wonder if there's any code, or preprocessing, that would allow me to create a new variable of "$job_title"?

Any ideas? Thanks.

gausarts’s picture

You may want to use field_get_items as suggested above. Below is placed inside preprocess_node:

 $node = $variables['node'];
 $author = user_load($node->uid);
 
 $job_titles = field_get_items('user', $author, 'field_job_title'); 
 $variables['job_title'] = !empty($job_titles[0]['value']) ? check_plain($job_titles[0]['value']) : '';

love, light n laughter

wooody’s picture

Thanks , work perfect.

kaztur’s picture

Hello! Great Thanks for the code, it works good!

I use this code for printing author fields in comments:

<?php $comment = $variables['comment'];
 $bizdir_titles = field_get_items('user', user_load($comment->uid), 'field_user_biz_dir'); ?>
 <div>BizDir: <?php print render($bizdir_titles[0]['value']); ?></div>

Everithing is good save the field 'field_user_biz_dir' is of type term reference and there's nothing printing on a page. Could You help me? please?

Skype: kaztur.ru
Phone: +7 917 871 09 85

wooody’s picture

thanks, works perfect

jonesui’s picture

For author profile info on the node:

<?php
      $node = $variables['node'];
      $author = user_load($node->uid);
      $first_name = field_get_items('user', $author, 'field_first_name');
      $last_name = field_get_items('user', $author, 'field_last_name');
      $userid = $node->uid;
?>
<?php if ($first_name && $last_name) : ?>
    <div class="author">
      <label class="inline">Author: </label>
      <a href="/user/<?php print $userid; ?>/contact">
        <?php print render($first_name[0]['value']); ?>
        <?php print render($last_name[0]['value']); ?>
      </a>
    </div>
    <?php endif; ?>

And for author info in comments:

<?php
      $comment_author = user_load($comment->uid);
      $first_name = field_get_items('user', $comment_author, 'field_first_name');
      $last_name = field_get_items('user', $comment_author, 'field_last_name');
      $userid = $comment->uid;
?>
<?php if ($first_name && $last_name) : ?>
      <div class="author">
        <label class="inline">By </label>
        <a href="/user/<?php print $userid; ?>/contact">
          <?php print render($first_name[0]['value']); ?>
          <?php print render($last_name[0]['value']); ?>
        </a>
      </div>
<?php endif; ?>
kaztur’s picture

mordaga, Great thanks for Your code!

It works good for fields with types as text or number, but it doesn't work with "term reference" type field - there's nothing shows. It shows only "By " text

Skype: kaztur.ru
Phone: +7 917 871 09 85

Jansel’s picture

I'm trying to customize a node--TYPE.tpl.php template, and I am wondering how I can print out a custom user profile field in it.
Basically, I have a user profile field called 'field_profile_photo', where a user enters his/her profile photo, I want a line that prints out the profile photo.
I have two content types is an article and the other profile, I want to print the photo of the profile of a user in my article

I hope someone can help me out here, that would be very kind. Thanks!

almamun’s picture

I'm using Taxonomy term fields for Job title, items of interest etc. and I can only retrieve term id, not the term name