基于node type为drupal添加不同的theme布局
07月 26th, 2010 by dreamer | Filed under drupal.这是一篇drupal theming相关的日志。
Drupal的每一个节点都拥有一个type:node type。如日志(blog entry), 论坛主题(forum topic),下载(downloading)。
默认的theme是为所有节点添加同一个page layout,很明显不能够满足期望使用drupal设计各种类型的网站的站长了。
而要不同的内容类型(node type)添加不同的layout(page.tpl.php)。
则需要稍微hack一下。
方法灰常简单:
找到theme所在的目录中的template.php文件(如果没有可以自己增加一个。) 在里面添加一段:
<?php
function phptemplate_preprocess_page(&$variables) {
if ($node = menu_get_object()) {
$variables['node'] = $node;
$suggestions = array();
$template_filename = 'page';
$template_filename = $template_filename . '-' . $variables['node']->type;
$suggestions[] = $template_filename;
$variables['template_files'] = $suggestions;
}
}
?>代码添加完毕之后,只需为不同的node type建议不同的layout文件到theme目录即可了,如:
forum -> page-forum.tpl.php
blog -> page-blog.tpl.php
book -> page-book.tpl.php
这样,访问不同类型的节点内容(node content),就可以读取对应的page layout鸟。
via:http://orzl.com/weblog/assign-page-layout-based-on-node-type-for-drupal