I created a node template for my content type which I only want to use for my default view mode and keep the standard view of the teaser. My file is named like node--customnode.tpl.php. As I read I can create a template file for the teaser by adding node--customnode--teaser.tpl.php but it does not work for default in the same way. Does anybody have an idea?
1 Answer
Try adding the following code to your theme's template.php
file:
function THEMENAME_preprocess_page(&$vars, $hook) {
if (isset($vars['node']->type) && isset($vars['view_mode']) && $vars['view_mode'] == 'default') {
$vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type . '__default';
}
}
Replace "THEMENAME" with the machine name of your theme.
This should allow you to use a template file called node--customnode--default.tpl.php
(assuming your content type is called "customnode") which will apply only on the default view mode.
n.b. You will need to flush your caches after adding both this functino and the template file.
-
the machine name of my theme is equal to the name of my .info file right? in my case the function name is my_bootstrap_subtheme_preprocess_page. Unfortunately it does not work, only a white screen appears when I call my site..– samAug 27, 2015 at 8:55
-
Sorry, there was a bracket in the wrong place in the code. I've fixed that now. Yes, the machine name is the same as the info file name (minus ".info") If you run into any further issues and get another white screen of death then you can temporarily add
error_reporting(E_ALL); ini_set("display_errors", 1);
in sites/default/settings.php (or even index.php) and you should see the errors, just make sure that doesn't added to your live site. Aug 27, 2015 at 12:46 -
Great, but I still have a problem. Now it does not use my template while rendering the content. May it is affected by my folder structure?! I use the bootstrap theme. The template.php file is in my theme folder (my_bootstrap_subtheme). My templates are placed in another sub folder called templates. I also named my template files as you said (with the correct content type names).– samAug 27, 2015 at 12:59
-
I tried a different function that I found in this post
function MYMODULE_preprocess_node(&$vars)
this works so far, when I don't use the IF-case which does not really make sense since I need it to distinguish the other types. It seems that my variable$vars['view_mode']
is empty. Do you may know why this could be the case? I also tried your code without the IF-case but it still didn't work.– samAug 28, 2015 at 9:14