[英]Drupal 7 how to render custom field
I've added a custom field called 'field_header' to the basic page content type. 我在基本页面内容类型中添加了一个名为“field_header”的自定义字段。 How do I access this field on the page.tpl.php template so I can display it wherever I want?
如何在page.tpl.php模板上访问此字段,以便我可以在任何地方显示它? Ideally I would like to remove it from $content as well.
理想情况下,我也希望将其从$ content中删除。 Thanks!
谢谢!
Don't forget not every page is necessarily a node page so you'd really be better off trying to access this in node.tpl.php
, not page.tpl.php
. 不要忘记不是每个页面都必定是一个节点页面,所以你最好不要尝试在
node.tpl.php
访问它,而不是page.tpl.php
。
In node.tpl.php
you can render the particular field like this: 在
node.tpl.php
您可以像这样呈现特定字段:
echo render($content['field_header']);
hide($content['field_header']); // This line isn't necessary as the field has already been rendered, but I've left it here to show how to hide part of a render array in general.
If you absolutely have to do this in page.tpl.php
then you want to implement a preprocess function in your template file to get the variable you need: 如果您必须在
page.tpl.php
执行此操作,则需要在模板文件中实现预处理功能以获取所需的变量:
function mymodule_preproces_page(&$vars) {
if ($node = menu_get_object() && $node->type == 'page') {
$view = node_view($node);
$vars['my_header'] = render($view['field_header']);
}
}
Then in page.tpl.php
you'll have access to the variable $my_header
which will contain your full rendered field. 然后在
page.tpl.php
您将可以访问变量$my_header
,它将包含完整的渲染字段。
In your node.tpl you have to use following code, for example field name : field_header 在node.tpl中,您必须使用以下代码,例如字段名称:field_header
<!-- For Showing only custom field's Value Use below code -->
<h2 class="title"><?php print $node->field_header['und']['0']['value'];?></h2>
<!-- ========================= OR ========================= -->
<!-- For Showing custom field Use below code , which shows custom field's value and title-->
<h2 class="title"><?php print render(field_view_field('node', $node, 'field_header')); ?></h2>
<!-- ========================= OR ========================= -->
<h2 class="title"><?php print render($content['field_header']); ?></h2>
从page.tpl.php您可以访问$ node,因此所有字段都来自$ node
print ($node->body['und']['0']['value']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.