We can theme page, block and node based on content type

ex.

* node-story.tpl.php would theme only story nodes.
* node-page.tpl.php would theme only page nodes. (Note that this is different from page.tpl.php which controls the layout of the entire page including header, sidebars, etc)
* node-forum.tpl.php would theme only forum nodes.
* node-book.tpl.php would theme only book nodes.

I try to do the same on comment, its not working.

comment-forum.tpl.php, I want to make my comment looks differnt for forum post and story. Can we do it ?

Comments

nevets’s picture

Note by default but there are some approaches that would allow you to theme based on content type.

In both cases it relies on the fact the one of the variables available is $comment which has nid field so you can get the node by

$node = node_load($comment->nid);
// type is now $node->type

One way to use this is in comment.tpl.php, you could modify it to get the node and add a class to the outer div that reflects the content type the comment applies to. At that point you could use CSS to style the comments differently.

It is also possible to provide template suggestions (so comment-forum.tpl.php would work). Since this is build into Drupal 6 I am going to assume you are using Drupal 5. For Drupal 5 I think this will work (untested)

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'comment:
   
        $comment = $vars['comment'];
        $node = node_load($comment->nid);

        $suggestions = array();
        $suggestions[] = 'comment-' . $node->type;

        $vars['template_files'] = $suggestions;
      }
      break;
  }
 
  return $vars;
}

This would either be added to your themes template.php file or if it already has _phptemplate_variables() deleclared the two functions would need to be merged.

feelexit’s picture

thank you so much.

BradM’s picture

If you want to go the comment.tpl.php route, here's what my comment.tpl.php file looks like (I've got a separate look for blog comments):

<?php $node = node_load($comment->nid); if ($node->type == 'blog') { ?>
	<!-- insert here how you want blog comments to look -->
<?php } else { ?>
	<!-- insert here how you want all other comments to look by default (existing comment.tpl.php content) -->
<?php } ?>

You can modify the if statement to include more node types, or expand it to add additional looks based on node type.

(thanks for the suggestions above... I was struggling with theming comments for a while, and finally got it working today. Wasn't successful going the template.php route)

Brad

moksa’s picture

Who can add this to http://drupal.org/node/11815

???

Thanks a lot !

ipwa’s picture

Nicolas
-------------------------