How can I theme nodes in the front page differently, that is to use different node.tpl.php with nodes displayed in the front page?

Comments

vsnguyen’s picture

This might not the be the best way to go about it, but I use the is_front() function to theme the node the for front page.

yuriy.babenko’s picture

In your theme's template.php, add:

	function _phptemplate_variables($hook, $vars = array())
	{
	    switch ($hook)
	    {
	        case 'node':
	        
	        	if(drupal_is_front_page())
	        	{
	            	$vars['template_files'][] = 'node-front';
	            }
	        
	        break;
	    }
	}

This will look for node-front.tpl.php before defaulting to node.tpl.php.

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

wpanssi’s picture

This worked for me!

--
http://www.sitemedia.fi

alisamar’s picture

is it works for Durpal 6.x ?

    function _phptemplate_variables($hook, $vars = array())
    {
        switch ($hook)
        {
            case 'node':
           
                if(drupal_is_front_page())
                {
                    $vars['template_files'][] = 'node-front';
                }
           
            break;
        }
    }
yuriy.babenko’s picture

No, the code snippet I posted is for Drupal 5.

Take a look at this: http://www.yubastudios.com/blog/drupal-extending-templating
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

NoCoolNamesRemain’s picture

This tutorial does not seem to specify how to do a custom node template for the 'front' page. How do we use this method to use a different node template file for the 'front' page?

kenorb’s picture

You can move this block into theme_preprocess_node() into template.php
and make sure that you have cleared cached each time before front page refreshment.
Try exit() to make sure that it's loaded for sure.

NoCoolNamesRemain’s picture

Edit /sites/all/themes/YOURTHEME/template.php to include:

function phptemplate_preprocess_node(&$vars) {
  //default template suggestions for all nodes
  $vars['template_files'] = array();
  $vars['template_files'][] = 'node';

  if (drupal_is_front_page()) {
    $vars['template_files'][] = 'node-front';
  }

  //individual node being displayed
  if($vars['page']) {
    $vars['template_files'][] = 'node-page';
    $vars['template_files'][] = 'node-'.$vars['node']->type.'-page';
    $vars['template_files'][] = 'node-'.$vars['node']->nid.'-page';
  }
  //multiple nodes being displayed on one page in either teaser
  //or full view
  else {
    //template suggestions for nodes in general
    $vars['template_files'][] = 'node-'.$vars['node']->type;
    $vars['template_files'][] = 'node-'.$vars['node']->nid;

    //template suggestions for nodes in teaser view
    //more granular control
    if($vars['teaser']) {
      $vars['template_files'][] = 'node-'.$vars['node']->type.'-teaser';
      $vars['template_files'][] = 'node-'.$vars['node']->nid.'-teaser';
    }
  }

  //echo "<pre>" . print_r($vars['template_files'], TRUE) . "</pre>"; //TESTING
}

Then copy /modules/node/node.tpl.php to /sites/all/themes/YOURTHEME/node-front.tpl.php and customize it to your needs.

Maine’s picture

This is an impressive helper function as it can be used for both page templates and node templates:

yuriy.babenko’s picture

You can apply this trick in pretty much any preprocessing function for any piece of themed content. I've got an example for blocks, too.

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

Tunox’s picture

Thanks for this snippet NoCoolNamesRemain.

Anyone would be kind enough to make it Drupal 7 ready?

Sorry for bumping up and old post, but google search rated this up high so future drupalistas might stumble upon this as well.

Regards

---
no signature is a good signature