March 27, 2012

Page–[node-type].tpl.php for Drupal 7

Drupal 7

You may have discovered that many places discuss that you can theme your drupal 7 template per node type by simply creating the template file:

page–[node-type].tpl.php

However, this is not avaialable in the current release of drupal 7 without adding a pre-processing hook to your template.php theme file.  In order to have this work, you must include something along the lines of:

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

You can then go about customizing your theme pages as desired by creating the before-mentioned tpl.php files.

Need more help with your drupal 7 development?  Feel free to contact our team to set up a consultation.

Jeremy Oms

Jeremy Oms

Jeremy is an entrepreneur and leading manager of New Blood, Inc. Excited about all things Internet related, Jeremy spends much of his time project managing custom web applications and directing advanced search engine optimization efforts.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

Related Posts

7 Comments »

  1. I just want to ask you about two ideas :

    1- what is the different between node–blog.tpl vs page–[node-type].tpl.php

    2- how can i test if the content type is teaser or full content

    thank you

    Comment by salam — July 31, 2012 @ 3:44 am

  2. Jeremy Oms

    1. page-[node-type].tpl.php will modify the display of the page output. See page template. node-blog.tpl will modify the node output. See the node tpl page. From a developer standpoint, you typically modify the page.tpl if you are looking to change the structure of the parts around the content. More the theme of the site. You would modify the node.tpl when you want to change how the nodes themselves are getting rendered. This might be changing the order of node parts are simply organizing them differently within the page structure.

    2. I am not sure about this answer. Maybe you are looking for this area:

    – Administer → Structure → Content types
    – click the “Manage fields” link for the content type
    – in the row for “Body”, click the “edit” link
    – uncheck “Summary input”
    – save the content type

    Or

    – Administer → Structure → Content types
    – click the “Manage display” link for your content type
    – drop down “Custom display settings”
    – uncheck “Teaser”
    – save the content type

    Comment by Jeremy Oms — July 31, 2012 @ 8:45 am

  3. Thank you,, you told me what the exact I’m looking for (different between node–blog.tpl and page–[node-type].tpl.php)

    because now, I build my own theme for drupal7 and I’m new to drupal

    and that’s a bit difficult for me .. and maybe it’ll take me along time :)

    Comment by salam — August 17, 2012 @ 9:27 am

  4. I added the function: myZen_preprocess_page(&$vars, $hook) that you proposed to my template.php but nothing happens. Where I’m wrong?

    Comment by Lucks — November 2, 2012 @ 5:45 am

  5. I tried what was suggested but when your replace the string with — it does not work. I removed this and simply let it use the __ instead and worked great. Great article though!

    Comment by Chad Peppers — January 2, 2013 @ 10:26 am

  6. Hello in the beginning that I am creating my first drupal theme and I have the following problem:

    I have my main website and I want it when you give to a main menu item, I leave other content and not how.

    I attached my function of template.php

    fidias_preprocess_page function (& $ vars, $ hook) {

    if (isset ($ vars ['node'])) {
    $ vars ['fidias_hook_suggestions'] [] = ‘page__’. str_replace (‘_’, ‘-’, $ vars ['node'] -> type);

    }
    }

    I think my problem is that it does not recognize the template.php

    I would appreciate your help

    Comment by Miguel Angel — April 25, 2013 @ 3:26 am

  7. Jeremy Oms

    Try flushing your cache within your drupal admin. This is the typical way to get drupal to recognize your new drupal theme files.

    Comment by Jeremy Oms — May 6, 2013 @ 1:36 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment