简体   繁体   English

如何在Drupal主题的模板文件内使用各个字段

[英]How to use individual fields inside template files of a Drupal theme

I'm new to Drupal and I'm creating an event management website with it. 我是Drupal的新手,并且正在用它创建一个事件管理网站。 I'm using Drupal 7 and I have created a custom content type called "event" and I have written my own theme. 我正在使用Drupal 7,并且创建了一个名为“事件”的自定义内容类型,并且编写了自己的主题。 I have a problem with theming the custom content type. 我对自定义内容类型进行主题设置有问题。 I have built a template file called node--event.tpl.php inside my theme folder so that it is used to display every event in the website. 我已经在主题文件夹中构建了一个名为node--event.tpl.php的模板文件,以便用于显示网站中的每个事件。 My problem is that I don't know how to use individual fields and put them in the places I want. 我的问题是我不知道如何使用单个字段并将其放在我想要的位置。 For example my event content type has an overview (text), image, date, and venue (getlocation field) and I want to use them individually in my template file so that I can put each one inside an HTML tag individually and not as a whole. 例如,我的事件内容类型具有概述(文本),图像,日期和地点(getlocation字段),并且我想在模板文件中单独使用它们,以便可以将每个单独放在HTML标记内,而不是整个。 I have searched Google but found nothing. 我已经搜索过Google,但一无所获。 I have used modules like Panel and Display Suite but I can't make what I want with them. 我曾经使用过Panel和Display Suite之类的模块,但无法使用它们来实现自己想要的功能。 Any solutions or hints would be appreciated a lot. 任何解决方案或提示将不胜感激。

Regards, Shakib 问候,Shakib

The following document will show you how to get access to the fields: 以下文档将向您展示如何访问这些字段:

https://api.drupal.org/api/drupal/modules!node!node.tpl.php/7 https://api.drupal.org/api/drupal/modules!node!node.tpl.php/7

All this information is stored in the $node array. 所有这些信息都存储在$ node数组中。 You can print the array to see it with print_r wrapped in html pre tags. 您可以打印数组以将其与包装在html pre标签中的print_r一起查看。

Example for a field named authors: 名为作者的字段的示例:

<?php $node->field_authors[0]['value'] ?> 

Or 要么

<?php $node->field_authors[0]['safe'] ?> 

for a sanitized version. 以获得清洁版本。

Instead of printing the array with print_r you can use the Devel module 可以使用Devel模块来代替使用print_r打印数组

Install and enable Development module and then you can use function dpm(). 安装并启用开发模块 ,然后可以使用函数dpm()。 This function prints a variable to the 'message' area of the page. 此函数将变量打印到页面的“消息”区域。 Try it: 试试吧:

<?php dpm($content); ?> 

Your field has to be placed here: 您的字段必须放在这里:

<?php dpm($content['field_authors']); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM