Drupal 6 - Adding an Above Comments Block to a node

In this Drupal tutorial, we will look at how to add a block which comes right before the comments for a node. At times it is necessary to add some content on a node but it should come before (above) the comments for that node. For example, you could want to display an advertisement or links for sharing or any other content like that.

Adding an above contents block can be done in 3 steps as the tutorial below shows.

Step 1 - Add the Above Comments Region to the Theme

Open the YOUR-THEME-NAME.info file (located in the folder that belongs to your theme) and add the following line, ideally where the other regions are:

regions[above_comments] = Above Comments

This tells Drupal to declare a region called "Above Comments" and make it available to the theme for us to use later.

Step 2 - Add a preprocess function to template.php

The next step is to configure the theme to prepare the output for the above comments block. We do this using preprocess as shown below. The following code should go in the "template.php" file which is located in the directory which belongs to the theme. Remember to change YOUR-THEME-NAME to the name of your theme:

function YOUR-THEME-NAME_preprocess_node(&$variables, $hook) 
{
  $variables['above_comments'] = theme('blocks', 'above_comments');
}

Step 3 - Output the variable on the page

The final step is to output the content of the region to the page. You do this in the same way you output the content of any region when theming. You add the appropriate print statement to the node.tpl.php file. If such a file does not exist in your theme directory you can add one using the template shown below:

<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
 
<?php print $picture ?>
 
<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
 
  <?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted; ?></span>
  <?php endif; ?>
 
  <div class="meta">
    <?php if ($taxonomy): ?>
      <div class="terms"><span class="metadesc">Filed under: </span><?php print $terms ?></div>
    <?php endif;?>
   </div>
 
  <div class="content clear-block">
    <?php print $content ?>
 
////////// start of addition
	<?php if ($above_comments): ?>
		<div class="above_comments"><?php print $above_comments ?></div>
	<?php endif ?>
////////// end of addition
 
  </div>
 
  <div class="clear-block">
    <?php if ($links): ?>
      <div class="links"><?php print $links; ?></div>
    <?php endif; ?>
  </div>
 
</div>

Please note that you will need to print the above_comments variable like you did above to all other node-type.tpl.php files if you want an above comments section to be available to them as well.

Note also that on the blocks page (at admin/build/block) you won't see the above comments section highlighted like the other regions. It will still come up in the list of regions though and you can assign regions to that block as per usual.



Joe Ebel said:

Nov 17, 2011 at 4:04 pm

Thanks for the info! It was much needed and was perfect for my situation!

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...