How to retrieve field value from another tpl in the template.php file for drupal 7?

710 views Asked by At

I need to retrieve the field values in 1 page.tpl.php on my template.php.

The normal way to retrieve a field in a page.tpl.php is: $field = field_get_items('node', $node, 'field_name'); $field_name = field_view_value('node', $node, 'field_name', $field[0]);

The page.tpl.php is normally located in the "sites\all\themes\themename\templates" directory

The template.php file is located in the "sites\all\themes\themename" directory

I got to pull the field values in page.tpl.php in my function located in template.php.

Can anyone suggest an appropriate way of doing this or even give me the working solution for this?

2

There are 2 answers

0
MilanG On

Load node that contains field you need first, with node_load() function and pass that node as parameter. You need node id for that, which can be hard-coded or acquired with view, collected from url, cookie, session or something like that.

0
Shabir A. On

Add a function hook_preprocess_page(&$var) and in the function get the node object data, use dsm module to reach to the node object and field. then assign that field to a variable like this $vars['new_var'] = $your_field

Clear cache and rebuild theme registry and print that variable in the page.tpl.php like <?php print $new_var; ?>