代码之家  ›  专栏  ›  技术社区  ›  Paul

使用特定的节点ID调用模板文件

  •  0
  • Paul  · 技术社区  · 14 年前

    Drupal中节点的标准模板是 node.tpl.php

    可以为一种内容类型调用不同的模板,如“新闻项”。你可以这样称呼它: node-newsitem.tpl.php .

    我想知道是否有一种方法可以调用特定的节点ID? node-34.tpl.php 工作。

    谢谢

    4 回复  |  直到 12 年前
        1
  •  3
  •   Aaron    14 年前

    在主题的template.php中,将以下内容放在theme_preprocess_node()的顶部:
    $vars['template_files'][] = 'node-'. $vars['node']->nid;

    因此,如果你的主题是“神话”,你可能会有以下几点:

    function myTheme_preprocess_node(&$vars){  
        $vars['template_files'][] = 'node-'. $vars['node']->nid;  
    }
    
        2
  •  6
  •   Dmytro mockaroodev    12 年前

    为了 德鲁巴7 使用此模板名称(34-is node id):

    node--34.tpl.php
    

    别忘了清空你的缓存!更多信息 on drupal.org

        3
  •  0
  •   Chris Ridenour    14 年前

    我们说话的时候我就有这个工作。在Drupal6中,我的首页是节点5。它使用

    page-node-5.tpl.php页

    如果没有加载,请考虑清除缓存或重建主题注册表。

        4
  •  -1
  •   peterjmag    14 年前

    这种命名约定将起作用,只是在默认情况下不会起作用。假设这是Drupal6,尝试将以下代码添加到主题的template.php中:

    /**
    * Override or insert variables into the node templates.
    *
    * @param $vars
    *   An array of variables to pass to the theme template.
    * @param $hook
    *   The name of the template being rendered ("node" in this case.)
    */
    function yourthemename_preprocess_node(&$vars, $hook) {
      $node = $vars['node'];
      $vars['template_file'] = 'node-'. $node->nid;
    }
    

    确保你不尝试重新申报 yourthemename_preprocess_node() --也就是说,如果它已经存在于主题的template.php中,只需添加$node和$vars['template_file']行即可。