【发布时间】:2016-10-24 19:49:33
【问题描述】:
在 Drupal 7 的 page.tpl.php 中创建一个子主题,并需要从自定义内容类型中提取 field_EXAMPLE 的值(纯文本),而不是其余内容的正常位置。
<!-- Adding $title as normal-->
<?php print render($title_prefix); ?>
<?php if (!empty($title)): ?>
<h1><?php print $title; ?></h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
<!-- THE ISSUE: Adding field_EXAMPLE -->
<h2> <?php print render($field_EXAMPLE;);?> </h2>
...
<!-- Where the rest of the content loads by default -->
<div><?php print render($page['content']); ?></div>
field_get_items 会起作用吗?
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
return isset($entity->{$field_EXAMPLE}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}
还是这个?
$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];
我要把这个放在page.tpl.php 吗?
试过了,但没有骰子。 -新手
这里是 var_dump(get_defined_vars());
["field_thestring"]=>
array(1) {
["und"]=>
array(1) {
[0]=>
array(3) {
["value"]=>
string(44) "This is a string of text please refer to me "
["format"]=>
NULL
["safe_value"]=>
string(44) "This is a string of text please refer to me "
}
}
}
【问题讨论】:
-
只是一个提示...有一个函数
get_defined_vars()可以用来查看tpl 文件中有哪些变量可用。因此,如果您启用了 devel 模块,您可以将dpm(get_defined_vars())放入 tpl 文件并重新加载页面以获得所有变量的漂亮列表。
标签: drupal drupal-7 drupal-theming drupal-fields drupal-entities