I am trying to get fields value in php but I cannot make it work. My code
<?php $value = $node->field_price; return $value; ?> Can somebody please help me to solve this? I believe it has to be something very simple.
You should first load your node with $node = node_load($nid). You can also use the Devel module (dpm(noad_load($nid))) to debug the node and make sure you're calling the right path to the value.
The values of fields are stored a bit deeper in $node->field_price, to be more specific they're stored like this:
$node->FIELD_NAME['LANGUAGE_CODE'][INDEX]['value'] The LANG_CODE is usually 'und' or undefined (if you run a multi-lang setup they correspond to the fields lang). The index specifies what exact value are you looking for (makes sense just for fields with cardinality higher than 1).
E.g. if you have a field with infinite cardinality that stores 3 taxonomy tags, you'll have three separate values under:
$node->FIELD_NAME['LANGUAGE_CODE'][0]['value'] $node->FIELD_NAME['LANGUAGE_CODE'][1]['value'] $node->FIELD_NAME['LANGUAGE_CODE'][2]['value'] 'und', use the LANGUAGE_NONE constant. Try this safer method:
$node = noad_load($nid); if (isset($node->field_price[LANGUAGE_NONE][0]['value'])) { $value = $node->field_price[LANGUAGE_NONE][0]['value']; }
$nodewhere it is not available, or it could be the node doesn't contain the field you are trying to access. When talking about code, it is very important to show which function contains the code and, in the case it is a custom hook used by a module, to say that.