我需要在 Drupal 7 中为 View 设置主题。有一个内容类型“书籍”,我需要列出 5 本书并以特殊方式(预览图像、标题和作者)为它们设置主题。
当我覆盖 views-view-field.tpl.php 并打印原始 SQL 结果时,我看到显示了所有字段。这段代码
echo "<pre>";
print_r($row);
echo "</pre>";
给予
[entity] => stdClass Object
(
[title] => ...
....
[nid] => 34
...
[body] => Array
...
但我不想将 [body] 从数据库传递到 php 端,因为它可能很大并且会导致性能问题。我没有在 View 设置中选择 [正文]。
有没有办法只将某些字段传递给 views-view-field.tpl.php?
提前致谢。
最佳答案
可用的变量写在 sites/all/modules/views/theme 文件夹文件中的文档中。
通常,您需要在 views-view-fields.tpl.php 模板上查看和修改的变量是 $fields
我使用 devel 模块 (http://drupal.org/project/devel) 查看可用的变量:
<?php
//after enabling the devel module...
dpm($fields);
// This will print a Kuomo display on the page with the array's vars
?>
一般来说,在节点 View 上,
<?php print $fields['title']->content; ?>
将打印节点标题。对于字段,尝试
<?php print $fields['field_FIELDNAME']->content; ?>
如果您有足够的内存力,您可以使用
捕获 Kuomo 中模板上可用的所有变量<?php dpm(get_defined_vars()); ?>
确保在尝试查看变量之前清除缓存。
关于performance - 如何正确设置 Drupal 7 中的 View 字段主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914220/