【问题标题】:How to retrieve field value from another tpl in the template.php file for drupal 7?如何从drupal 7的template.php文件中的另一个tpl检索字段值?
【发布时间】:2015-06-12 11:00:23
【问题描述】:

我需要在我的 template.php 上检索 1 page.tpl.php 中的字段值。

在 page.tpl.php 中检索字段的正常方法是: $field = field_get_items('node', $node, 'field_name'); $field_name = field_view_value('node', $node, 'field_name', $field[0]);

page.tpl.php 通常位于“sites\all\themes\themename\templates”目录中

template.php 文件位于“sites\all\themes\themename”目录下

我必须在 template.php 中的函数中提取 page.tpl.php 中的字段值。

任何人都可以提出一个适当的方法来做这件事,甚至给我一个可行的解决方案吗?

【问题讨论】:

    标签: templates drupal drupal-7 drupal-nodes


    【解决方案1】:

    使用 node_load() 函数加载包含您首先需要的字段的节点并将该节点作为参数传递。你需要节点 ID,它可以硬编码或通过视图获取,从 url、cookie、会话或类似的东西中收集。

    【讨论】:

      【解决方案2】:

      添加函数hook_preprocess_page(&$var) 并在函数中获取节点对象数据,使用 dsm 模块到达节点对象和字段。然后将该字段分配给像这样的变量$vars['new_var'] = $your_field

      清除缓存并重建主题注册表并在 page.tpl.php 中打印该变量,如 <?php print $new_var; ?>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-01
        • 1970-01-01
        相关资源
        最近更新 更多