How to print profile fields in Drupal 7

The Best method: using drupal_render field_view_field

Note: "profile_main" and "field_birthdate" are used as example, you can change them accordingly.
Print fields in node.tpl.php (or node--100.tpl.php)
node--100.tpl.php means a webpage node/100

<?php
global $user;
$uid = user_load($user->uid);
$profile_main = profile2_load_by_user($uid, 'main');
?>


<?php
print drupal_render(field_view_field('profile2', $profile_main, 'field_birthdate', 'value'));
?>

This method will print the labels also.
Print fields in user-profile.tpl.php
<?php
// load profile fields in user-profile.tpl.php
$profile = profile2_load_by_user($elements['#account']);
?>


<?php
print drupal_render(field_view_field('profile2', $profile['main'], 'field_birthdate', 'value'));
?>

$profile['main']: 'main' is your profile machine name.

See here:
http://coder1.com/articles/printing-or-rendering-node-field-or-profile2-...

Another method: Printing Fields

The following instructions are for printing fields in profile2.

Note: your can replace "$profile_somename" with your profile name.
Or if you want to print a normal cck field (not profile filed), you just replace "$profile_somename" with "$node".

How to display a node title from a node reference field?

<?php
global $user;
$uid = user_load($user->uid);
$profile_somename = profile2_load_by_user($uid, 'somename');
?>


<?php
$nid
= $profile_somename->field_course3['und'][0]['nid'];
$node = node_load($nid);
print (
$node->title);
?>

How to print a text field?


<?php print ($profile_somename->field_mark3['und'][0]['value']); ?>

How to display a taxonomy term from a reference field?

<?php
    $title
= $profile_somename->field_bamajor['und'][0]['tid'];
   
$sql = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid = :title', array(':title' => $title));
   
$result = $sql->fetch();
    if (!empty(
$result)) { $title = $result->name; }
        print
$title;
?>

How to print a reference image field:

<?php
$my_image
= array(
 
'style_name' => 'medium', //the image style created
 
'path' => $profile_somename->field_photo['und'][0]['uri'],
);
print
theme('image_style', $my_image);
?>

Print a date field

<?php print date('M j, Y', strtotime(str_replace('T', ' ', $profile_somename->field_birthdate['und'][0]['value']))); ?>

How to print an image field:

<?php print image_style_url(‘medium’, $profile_somename->field_myimage[‘und’][0][‘uri’]); ?>

How to print an file field:

<?php print $profile_somename->field_mp3file['und']['0']['filename']; ?>

How to print an file field with URL link:

<?php print file_create_url($profile_somename->field_mp3file['und']['0']['uri']); ?>

Note: your can replace "$profile_somename" with $node, if you want to print a normal cck field (not profile)

Others

To print a file field description

<?php
print render($content['field_myfile']['#items'][0]['description']);
?>

Display a view in node

This is good for front page display (frontpage.tpl.php):

<?php
print views_embed_view('viewname');
?>

This is a great stuff

Thanks you so much. A question if I may ...

Re: How to display a taxonomy term from a reference field? ... I am using this to create css classes and am wondering if it could possibly be modified to convert the term to lowercase?

ensk

very helpfull information but i have 3 roles how can i solve this?
if replace 'main' for $uid->roles[8] work fine for this role but the other roles name are in another position of the array $uid->roles[9] and $uid->roles[10].... i don't know how to solve this ....
but your information is very good thanks you