【问题标题】:Drupal 7 CCK fields deconstruction from $content in node.tpl.php fileDrupal 7 CCK 字段从 node.tpl.php 文件中的 $content 解构
【发布时间】:2012-03-24 20:40:21
【问题描述】:

经过漫长的探索,终于在node.tpl.php中找到了Drupal 7中$content拆分的解决方案。

以前我用过我用过:

print $node->field_name['und'][0]['value'];

这是在发出警告:

Notice: Undefined offset: 0 in include() (line 24 of C:\xampp\htdocs\drup\sites\all\themes\myCustomTheme\node.tpl.php)

现在我正在使用下面的函数,它可以正常工作,没有错误

$output= field_get_items('node', $node, 'field_name');
$output = $output[0]['safe_value'];
print $output;

现在的问题是我的页面上有超过 50 个字段,我认为调用 field_get_items('node', $node, 'field_name') 函数 50 次效率不高。

什么是替代品? 或者我应该对 field_get_items 函数严格吗 或者我应该严格打印 $node->field_name['und'][0]['value'];我听说在 Drupal 7 中使用它不好,因为 und 是未定义的。这虽然解决了我的问题,但给出了令人讨厌的警告。

【问题讨论】:

    标签: views drupal-7 cck


    【解决方案1】:

    在您的模板中,您应该可以访问之前预处理过的变量。

    您的每个字段实际上都是一个变量,因此您可以执行 $field_name[LANGUAGE_NONE][0]['safe_value']

    为了摆脱错误提示,我建议你使用 isset() 函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多