在模板中的load_node上访问Drupal 7中的字段值

时间:2011-07-03 17:03:30

标签: php drupal drupal-7 drupal-theming

在内容类型的模板中,我从节点引用加载节点。

它加载,如果我做print_r,我得到这个:

stdClass Object ( 
  [vid] => 40 
  [uid] => 14 
  [title] => Cover 
  [log] => 
  [status] => 1 
  [comment] => 0 
  [promote] => 1 
  [sticky] => 0 
  [nid] => 40 
  [type] => portfolio_image_main 
  [language] => en 
  [created] => 1309382711 
  [changed] => 1309382711 
  [tnid] => 0 
  [translate] => 0 
  [revision_timestamp] => 1309382711 
  [revision_uid] => 14 
  [field_portolio_image] => Array ( 
    [en] => Array ( 
      [0] => Array ( 
        [fid] => 5626 
        [alt] => 
        [title] => 
        [uid] => 14 
        [filename] => Cover.jpg 
        [uri] => public://Cover.jpg 
        [filemime] => image/jpeg 
        [filesize] => 147898 
        [status] => 1 
        [timestamp] => 1309382711 
      ) 
    ) 
  ) 
  [name] => jojo 
  [picture] => 0 
  [data] => a:1:{s:7:"contact";i:1;} 
) 

我试图在这里访问单个变量:

$newImagePath1 = $newImage1->field_portfolio_image['en '][0]['filename'];

但到目前为止没什么。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

请尝试使用以下代码

$keys = array_keys($arr[field_portolio_image][en]);
$arr[field_portolio_image][en][$keys][filename]; 

答案 1 :(得分:1)

有一个辅助函数可以访问用户正确语言的字段项(否则,您必须对['en']部分进行硬编码。)

field_get_items()

所以你的代码最终会是这样的:

$field_instances = field_get_info('node', $newImage1, 'field_portfolio_image');
// $field_instances should now be an array.
foreach ($field_instances as $field_instance) {
  print $field_instance['filepath'];
}