简体   繁体   English

Drupal 7如何覆盖特定内容类型的page.tpl?

[英]Drupal 7 How to override page.tpl for specific content type?

I wanted to override page.tpl.php for specific content type. 我想覆盖page.tpl.php以获取特定的内容类型。 I have tried these thing, nothing works for me. 我试过这些东西,没有什么对我有用。

  1. page--article.tpl.php
  2. page--node--article.tpl.php
  3. page--node--type--article.tpl.php
  4. page--node-type--article.tpl.php
  5. page--type--article.tpl.php

But when I targetted specific node by number ie page--node--8.tpl.php it worked fine. 但是,当我按编号(即page--node--8.tpl.php特定节点时,它工作正常。 I think page--article.tpl.php should have worked, but I dont know why its not working. 我认为page--article.tpl.php应该page--article.tpl.php ,但我不知道为什么它不起作用。

Please tell me if I am naming it wrong. 如果我说错了,请告诉我。 and how can I debug such things. 我该如何调试这些东西。 I have heard I can use Devel module, but know nothing about it. 我听说我可以使用Devel模块,但对此一无所知。 A slight hint in right direction will be appreciated. 在正确的方向略有提示将不胜感激。

Thanks in advance 提前致谢

You have to make sure that your template can handle this ... I got a code snippet that worked for me here: 你必须确保你的模板可以处理这个...我有一个代码片段,在这里为我工作:

http://drupal.org/node/1089656#comment-4426790 http://drupal.org/node/1089656#comment-4426790

<?php
function themeName_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) {
    // If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
  }
}
?>

Have you remembered to clear the cache (under Administer > Site configuration > Performance) so that Drupal is 'aware' of your new file & uses this rather than the default. 您是否记得清除缓存(在管理>站点配置>性能下),以便Drupal“了解”您的新文件并使用此而不是默认值。

See here on Clearing cached data 请参阅清除缓存数据

Also, you may need to add a preprocess hook (I haven't used D7 myself, just 5/6 — think this has changed slightly.) This post on Drupal Stack Exchange seems to give more details 此外,您可能需要添加一个预处理挂钩(我自己没有使用过D7,只有5/6 - 认为这已经略有改变。) Drupal Stack Exchange上的这篇文章似乎提供了更多细节

I just ran into the same problem. 我刚遇到同样的问题。

node--recipe.tpl.php 节点 - recipe.tpl.php

not

page--recipe.tpl.php 页面 - recipe.tpl.php

this is, of course, with a content type called 'recipe'. 当然,这是一种名为'recipe'的内容类型。

ALWAYS CLEAR THE CACHE... 总是清除缓存......

Go to /admin/config/development/performance and click the clear cache button. 转至/ admin / config / development / performance并单击清除缓存按钮。 It should be the first thing you do if you've made alterations to the structure of the backend and they're not showing as expected. 如果您对后端的结构进行了更改并且它们没有按预期显示,那么应该首先执行此操作。

Otherwise you almost had it right 否则你几乎没事

  1. page--content-type-name.tpl.php 页面 - 内容型name.tpl.php

will allow you to theme the content type template. 将允许您主题内容类型模板。

I know this is an old question but if others arrive here, the way I override content types in drupal 7 is like this 我知道这是一个老问题但如果其他人到达这里,我在drupal 7中覆盖内容类型的方式就是这样

node-- contenttype .tpl node-- contenttype .tpl

You will need to overwrite the specific node template for the content type if it is anything different than a Basic Page. 如果内容类型与基本页面不同,则需要覆盖特定节点模板。 Please check this out: 请检查一下:

http://api.drupal.org/node/19080 http://api.drupal.org/node/19080

Under this page copy/paste the items that are in the node.tpl.php page and overwrite it on how you want. 在此页面下,复制/粘贴node.tpl.php页面中的项目,并根据您的需要覆盖它。 You can access the node specifically by $node 您可以通过$ node专门访问该节点

Your template for articles would be: 您的文章模板将是:

node--article.tpl.php 节点 - article.tpl.php

Once this is created make sure you clear cache to ensure this works. 创建后,请确保清除缓存以确保其有效。

So depending on what you are trying to accomplish, there might be some CSS tricks that could help you out. 因此,根据您要完成的任务,可能会有一些CSS技巧可以帮助您。

For instance, to override any CSS rules for a specific node type just remember that your pages for that node type will have a body class of "node-type-[TYPE]" (ie: .node-type-article ) 例如,要覆盖特定节点类型的任何CSS规则,请记住该节点类型的页面将具有"node-type-[TYPE]"的主体类(即: .node-type-article

You can then override the CSS as follows: 然后,您可以按如下方式覆盖CSS:

body.node-type-article #selector {}

Step 1.create a content type for example: my content type is "fullwidthpage" 步骤1.创建内容类型,例如:我的内容类型是“fullwidthpage”

Step 2. copy page.tpl.php and rename to page--fullwidthpage.tpl in template folder. 步骤2.复制page.tpl.php并重命名为page - fullwidthpage.tpl在模板文件夹中。

Step 3. Add this code to template.php 步骤3.将此代码添加到template.php

function <strong>YOUR_THEME_NAME_HERE</strong>_preprocess_page(&$variables) {
  if (isset($variables['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
  }
}

Don't forgot to change YOUR_THEME_NAME_HERE 别忘了更改YOUR_THEME_NAME_HERE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM