0

enter image description hereI added a new field(First name) in Account settings under Configuration & its machine name is 'field_first_name'
I am trying to load this value in home page like this

<?php print $user->field_first_name ?> 

But the above code is not working. Please help. I debug $user and found that this value isn't there. What should I do to show the value of the newly added field in home page.

I've added screenshot.

1
  • You seems to first need to debug the $user, to do that: dpm($user); (if you have devel module installed), otherwise echo '<PRE>'; print_r($user);, than you can check what is the exact name of field first name in the user object. Commented Oct 14, 2013 at 11:20

3 Answers 3

2

You should use field_get_items:

global $user; $user_contaning_field = user_load($user->uid); // Check if we're dealing with an authenticated user if ($user->uid) { // Get field value from $user $field_first_name = field_get_items('user', $user_contaning_field, 'field_first_name'); foreach ($field_first_name as $key => $value) { print field_view_value('user', $user_containing_field, 'field_first_name', $field_first_name[$key]); } } 
6
  • And better, use field_view_value to get the value. Commented Oct 14, 2013 at 11:07
  • I am getting error message "Notice: Undefined variable: field_first_name in include() (line 31 of C:\xampp2\htdocs\Journal\themes\admin\templates\includes\admintoolbar.php)." Commented Oct 14, 2013 at 11:17
  • @SureshAlagar please try with updated answer Commented Oct 14, 2013 at 11:29
  • the reason it was not showing you field is $user used as arguments for "field_get_items" does not contain the field information.you to load the user so to have the field inside the user entity. Commented Oct 14, 2013 at 11:31
  • Thanks, arpitr. You have solved my problem. Thanks again. Commented Oct 14, 2013 at 12:15
2

Small edit with @Beebee asnwer, since it seems he is not logged in so adding it as separate answer

global $user; // load the user entity so to pick the field from. $user_contaning_field = user_load($user->uid); // Check if we're dealing with an authenticated user if($user->uid) { // Get field value; $field_first_name = field_get_items('user', $user_contaning_field, 'field_first_name'); if(isset($field_first_name[0]['value'])) print $field_first_name[0]['value']; } 
0

Try this for starters

<?php global $user; if ($user->uid) { // if registered... //get field value and print it } 
3
  • I am getting an error message "Undefined property: stdClass::$field_first_name" Commented Oct 14, 2013 at 11:03
  • yeah its actually not the real code you need, is a start point, try to debug the $user object..., field_get_items below seems great Commented Oct 14, 2013 at 11:07
  • Hello. Code-only answers are rarely usable. If the code works, they don't tell what was the problem and why it works. If it does not, it's even worse as they provide neither reasoning nor solution. Commented Oct 14, 2013 at 11:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.