Hi,

I would like to print some Profile2 fields in my node.tpl.php template.
What would be the best way to achieve this?

Thank you!

Comments

youssefr’s picture

Did you try this...


global $user;

$uid = user_load($user->uid);
$profile = profile2_load_by_user($uid, 'customer_profile');

echo $profile->field_firstname['und'][0]['value'];
echo $profile->field_lastname['und'][0]['value'];
echo $profile->field_phone['und'][0]['value'];
//...etc

quasi’s picture

Hello youssefr and thank you,

I tried your solution but it doesn't print out anything.
I changed the 'customer_profile' and the fields names.

Thank you again!

quasi’s picture

Were you able to print the fields in this way?

quasi’s picture

This is how I got it working:

  $profile = profile2_load_by_user($uid, 'customer_profile');
  //print the whole array with the devel module
  dprint_r($profile)
  //print field_firstname
  echo $profile->field_firstname['und'][0]['value'];
  //...etc

Thanks again youssefr for pointing me in the right direction!

quasi’s picture

Status: Active » Closed (works as designed)
drupalina’s picture

Status: Closed (works as designed) » Active

Hi,
I'm trying to do something similar: to print author's Telephone number (field_telephone) on node teasers of the nodes that they author.
I tried both examples:

  <div class="telephone">
  <?php
global $user;
$uid = user_load($user->uid);
$profile = profile2_load_by_user($uid, 'business_profile');
print t('<strong>Tel: </strong>');
print render($profile->field_telephone['und'][0]['value']);
?>
</div>
<?php
  $profile = profile2_load_by_user($uid, 'business_profile');
  //print field_firstname
  echo $profile->field_telephone['und'][0]['value'];
?>

They both work for authenticated users, but for user 1 it shows

Notice: Trying to get property of non-object in include() (line 66 of C:\wamp2\www\drupalsite\sites\all\themes\simpler\node--property.tpl.php).

and returns no values.

Please help.

drupalina’s picture

Status: Active » Closed (fixed)

got it working using

  <?php
global $user;
$uid = user_load($node->uid);
$profile = profile2_load_by_user($uid, 'business_profile');
print t('<strong>Tel: </strong>');
print render($profile->field_telephone['und'][0]['value']);
?>

note the difference between $user->uid and $node->uid

faqing’s picture

drupalina,

It only works for text feild, but does not work for node reference, term reference.
I mean if your "field_telephone" is a reference link (link to another node), the following is not working:

print render($profile->field_telephone['und'][0]['value']);

but change value to nid, it will pring node number.

print render($profile->field_telephone['und'][0]['nid']);

Do you know how to render the reference node?

Number Nine’s picture

Status: Closed (fixed) » Active

Hello faqing,

I am using this code to render Profile2 text fields:

 $profile = profile2_load_by_user($elements['#account']); 
print $profile['my_profile_name']->field_name['und'][0]['value']; 

The thing is, I cannot apply what you indicate in your comment here above to render node references.
I tried something like this but it did not work:

print render($profile->field_name['und'][0]['nid']);

Thank you for your help if you or anyone alse can.

faqing’s picture

Status: Active » Closed (fixed)

Hello,
We are discussing on printing profile fields on node.tpl.php template.

For user-node.tpl.php, check here
http://drupal.org/node/1178260

For node reference field and taxonomy field:
http://thanhsiang.org/faqing/node/178

Number Nine’s picture

Thank you Faqing, I see your reply only now.
The project is in a stand-by position and will re-start soon.
Meanwhile I'll look into your suggested posts.