I have some code (calling JS and CSS) in the head section that I only want to appear if the HTML template is used for the content type 'home page'.

I did it like this in the html template:

<?php if ($variables['node']->type = 'home_page'){ ?> 
<?php } ?> 

But if I check the source code of my pages, the code is showing up in all off my pages, not only on the pages that use the 'home page' template.

Any tips, code, links?

PS: I'm not good at PHP (I normally cut and paste)

4 Answers 4



if ($variables['node']->type = 'home_page'){

change it into:

if ($variables['node']->type == 'home_page'){

  • Thanks for quick response! But if i do it like this, the code is not showing up on the homepages ( there are 5 pages that use the homepage template). :-s But it shows up on all the other pages... Oct 29, 2015 at 12:34
  • is home_page your custom content type? Oct 29, 2015 at 12:39
  • I don't think so. www.hotelcapitole.be is the website... In source code you see comment where i want to show up my extra code... but space is empty. But if i look at other pages, the code is showing up... Gonna try some things now.. Oct 29, 2015 at 12:44
  • how about home-page? try to var_dump($variables['node']->type) and then you will have the right content type for your condition. Clear cache after changes. Oct 29, 2015 at 12:47

You can do it by using hook_init

 * Implementation of hook_init()
function Hook_init() {

     // Check for front page
        // Add css/js files
       drupal_add_css($module_path . '/css.css');
       drupal_add_js($module_path . '/js.js');

If you add in template file look this code :

function yourtheme_preprocess_node(&$vars) {
  // Add JS & CSS by node type
  if( $vars['type'] == 'your-node-type') {
    drupal_add_js( /* parameters */ );
    drupal_add_css( /* parameters */ );

  // Add JS & CSS to the front page
  if ($vars['is_front']) {
    drupal_add_js( /* parameters */ );
    drupal_add_css( /* parameters */ );

  // Add JS & CSS by node ID
  if (drupal_get_path_alias("node/{$vars['#node']->nid}") == 'your-node-id') {
    drupal_add_js( /* parameters */ );
    drupal_add_css( /* parameters */ );

IF you are trying to include the code only on the front page of your site, I'd suggest you override its template by creating page--front.tpl.php inside the template directory of your theme. See this explanation.

EDIT: If you want to use the logic you had in your posting, I would be sure to check the syntax of your if statement. I may be wrong about this, but I've mostly seen this as:

<?php if ($variables['node']->type == 'home_page'): ?> 
Html, css or Javascript code here. 
<?php endif; ?>. 

Notice, no curly braces. Also, as another answer pointed, it's ==

  • In this case your suggested solution is not optimal. Normaly I use the page--front.tpl.php . But i built the website using a normal node tpl for the home-pages... 5 times.. one for dutch .. one for english... all use the called 'home_page' tpl... Oct 29, 2015 at 13:05

If you are putting condition for your sites home page its better to use : if(drupal_is_front_page()){
CODE... }

Alternative is:

If you print all your $variables array you will find $variables['is_front'] set to either true or false which you can use.

Just remember to set site's front page.

Not the answer you're looking for? Browse other questions tagged or ask your own question.