"根据内容类型定制drupal模板"

Published on Aug. 22, 2023, 12:06 p.m.

找到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