当自定义模板已应用于节点的内容类型时,如何在 Drupal 6 中自定义特定节点?
[对于 Drupal 6] 假设我创建了一个名为“my_content_type”的内容类型。我可以通过创建“page-node-my_content_type.tpl.php”来覆盖整个内容类型的默认模板。但是,进一步自定义该内容类型的单个节点(例如,节点 5555)的最佳方法是什么?
我尝试了以下方法,但没有一个起作用:
- page-node-5555.tpl.php
- page-node-my_content_theme-5555.tpl.php
- node-5555.tpl.php
这些都不起作用。他们都继续使用我原来的内容类型模板。
[For Drupal 6] Let's say I've created a content type called "my_content_type". I can override the default template for that entire content-type by creating "page-node-my_content_type.tpl.php". But, what would be the best way to then further customize a single node of that content type (e.g., node 5555)?
I tried the following, but none worked:
- page-node-5555.tpl.php
- page-node-my_content_theme-5555.tpl.php
- node-5555.tpl.php
None of these work. They all continue to use my original content-type template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Drupal 的页面模板在建议系统上工作。根据当前 URL,创建一组可能的模板文件。它循环遍历数组(以相反的顺序)寻找存在的模板文件。它找到的第一个,就会使用。
drupal 的主题系统为您提供了一个钩子来修改模板建议。打开您的 template.php 并找到
$vars 变量就是包含建议的内容,特别是 $vars['template_files']
默认情况下唯一可用的页面建议是
据我所知,page-node-[node_type].tpl.php 默认情况下不起作用,所以它可能您已经修改了 preprocess_page 模板以添加此功能。
但是,如果您想添加更具体的模板,您可以这样做...
这将允许模板建议的以下层次结构
Drupal's page templates work on a suggestion system. Based on the current URL, an array of possible template files is created. It loops through the array (in reverse order) looking for template files that exists. The first one it finds, it will use.
drupal's theme system provides a hook for you to modify the template suggestions.. open up your template.php and find
the $vars variable is what contains the suggestions, specifically $vars['template_files']
By default the only page suggestions that are available are
As far as im aware, page-node-[node_type].tpl.php does not work by default, so its likely you have already modified the preprocess_page template to added in this functionality.
However if you want to add more specific templates you could do something like this...
this will allow the following hierarchy of template suggestions
在 Drupal 7 中,只需复制
page.tpl.php 模板
并将其重命名为page--node--[node:id].tpl.php
清除缓存并开始调整..
In Drupal 7 just copy the
page.tpl.php template
and rename it aspage--node--[node:id].tpl.php
Clear cache and start tweaking..
此代码应该不起作用,因为 hook_preprocess_page() 不会传递任何节点信息。 hook_preprocess_node() 确实如此。因此您可以轻松地创建自定义node.tpl,但无法轻松地为特定节点创建自定义page.tpl。无论如何我都无法弄清楚:)
稍后...
在默认的 Drupal 中,page-node-NID.tpl.php 将无需特殊编码即可工作。然而,在我的网站上,它不起作用,我使用以下代码使其工作:
This code should not work because hook_preprocess_page() does not get passed any node information. hook_preprocess_node() does. So you can easily create a custom node.tpl, but you cannot easily create a custom page.tpl for a specific node. Not that I've been able to figure out anyway :)
Later...
In default Drupal, page-node-NID.tpl.php will work with no special coding. On a site of mine, it wasn't working, however, and I used the following code to make it work: