将节点中的字段添加到page.tpl.php,drupal 7
在Drupal 7的将节点中的字段添加到page.tpl.php,drupal 7,drupal,drupal-7,drupal-theming,drupal-fields,drupal-entities,Drupal,Drupal 7,Drupal Theming,Drupal Fields,Drupal Entities,在Drupal 7的page.tpl.php中创建一个子主题,并需要从字段中提取值(纯文本),例如从自定义内容类型中提取值(纯文本),而其他内容则不在正常范围内 <!-- Adding $title as normal--> <?php print render($title_prefix); ?> <?php if (!empty($title)): ?> <h1><?php print $t
page.tpl.php
中创建一个子主题,并需要从字段中提取值(纯文本),例如从自定义内容类型中提取值(纯文本),而其他内容则不在正常范围内
<!-- 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>
还是这个
$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];
我是否将其放入page.tpl.php
?
我试过了,但没有掷骰子-新手
这里是var_dump(get_defined_vars())
$node=node\u加载($nid);
$example_value=$node->field_示例[$node->language][0]['value'];
或者
$node=node\u加载($nid);
$values=字段\获取\项($node',$node,'EXAMPLE');
如果($values!=FALSE){
$val=$values[0]['value'];
}
否则{
//没有结果
}
您可以将代码直接放入页面模板中,但也可以将其放入页面预处理挂钩中,设置模板变量并使用页面模板中的该变量:
这是一种更干净的方式,但两者都应该有效
另外,如果前端没有立即看到更改,请尝试删除Drupal的缓存
要获取节点id,请尝试:
global $node;
$nid = $node->nid;
$node = node_load($nid);
...
如果这不起作用,试试这个:
if ($node = menu_get_object()) {
// Get the nid
$nid = $node->nid;
$node = node_load($nid);
...
}
或者这样:
if (arg(0) == 'node' && is_numeric(arg(1))) {
// Get the nid
$nid = arg(1);
$node = node_load($nid);
...
}
您可以使用预处理将值添加到传递给模板的$variables中
在template.php中:
MYTHEMENAME_preprocess_page(&variable){
$values = current(field_get_items('node', $variable['node'],'field_machine_name'));
$variable['myvar'] = $values['value'];
}
在template.tpl.php中
echo $myvar; // contains your value
假设您创建了一个名为field\u thestring
的字段,要在主题
之外的位置为内容类型文章
的页面呈现该字段
第一步。复制主题的page.tpl.php。并将其重命名为page--article.tpl.php
第二步。在page.var.php
中
function THEME_preprocess_page(&$variables) {
// To activate page--article.tpl.php
if (isset($variables['node']->type)) {
$nodetype = $variables['node']->type;
$variables['theme_hook_suggestions'][] = 'page__' . $nodetype;
}
// Prevent errors on other pages
if ($node = menu_get_object()) {
if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}
第三步。在页面--article.tpl.php
添加
最初,我想要求所有内容类型都有另一个字段,因为所有内容类型都有一个标题。确定这不是进一步发展的好主意
只是一个提示。。。有一个函数get_defined_vars()
,可用于查看tpl文件中有哪些变量可用。因此,如果您启用了devel模块,您可以将dpm(get_defined_vars())
放入tpl文件并重新加载页面,以获得所有变量的漂亮列表。我怀疑是否需要加载节点,因为它可能已经在变量中。请参阅我对上面的get_defined_vars()
函数的评论。根据BVSK的问题,需要在page.tpl中提取其他节点字段的值。所以加载节点是必要的。@VivekKumar我在page.tpl.php
中的主题预处理页面()中得到一个错误`Undefined variable:nid',用于此行$node=node\u加载($nid)代码>EntityFormedException在节点类型的实体上缺少bundle属性在实体_extract_id()
第7907行…\ROOTFOLDER\includes\common.inc)
@BVSK中,您必须传递要呈现的节点id,或者在文章和自定义内容类型之间必须存在某种关系才能获取节点id。它不喜欢$nid=$node->nid代码>。注意:尝试在THEME\u preprocess\u page()
Ok中获取非对象的属性,然后尝试其他选项。首先获取节点id,然后根据需要自行加载节点。
if (arg(0) == 'node' && is_numeric(arg(1))) {
// Get the nid
$nid = arg(1);
$node = node_load($nid);
...
}
MYTHEMENAME_preprocess_page(&variable){
$values = current(field_get_items('node', $variable['node'],'field_machine_name'));
$variable['myvar'] = $values['value'];
}
echo $myvar; // contains your value
function THEME_preprocess_page(&$variables) {
// To activate page--article.tpl.php
if (isset($variables['node']->type)) {
$nodetype = $variables['node']->type;
$variables['theme_hook_suggestions'][] = 'page__' . $nodetype;
}
// Prevent errors on other pages
if ($node = menu_get_object()) {
if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}