Custom Drupal Theme Block Regions

posted Jun 22, 2011, 11:42 PM by admin@ revitviet.vn

I recently added Google AdSense ads to this site to see what the click-through-rate will be. I wanted to put a banner ad above the content section you are reading right now in addition to side blocks. Since I'm using the bluemarine theme, there was not a way to put blocks into the section above the main content, so I modified the bluemarine theme to make this happen.

It's very easy to add/change block regions in a theme. Here's the steps for modifying the bluemarine theme:

1) Copy the bluemarine directory (with all its files) to sites/all/themes and rename the directory, e.g. mybluemarine

2) Go to that new directory and rename the bluemarine.info file to match your directory name, e.g. mybluemarine.info

3) Add the regions to the .info file, e.g.

 
regions[header] = Header
regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content_top] = Above content
regions[content_bottom] = Below content
regions[footer] = Footer

4) Modify the page.tpl.php file to match the regions from (3). In this case, the header, left, right, and footer regions are the same as before. I've added the content_top and content_bottom regions, so I will add them to the page template. e.g.

Before:

 
        <?php print $breadcrumb ?>
        <h2 class="title"><?php print $title ?></h2>
        <div class="tabs"><?php print $tabs ?></div>
        <?php if ($show_messages) { print $messages; } ?>
        <?php print $help ?>
        <?php print $content; ?>
        <?php print $feed_icons; ?>

After:

 
        <?php print $breadcrumb ?>
        <?php print $content_top ?>
        <h2 class="title"><?php print $title ?><h2>
        <div class="tabs"><?php print $tabs ?></div>
        <?php if ($show_messages) { print $messages; } ?>
        <?php print $help ?>
        <?php print $content; ?>
        <?php print $feed_icons; ?>
        <?php print $content_bottom ?>

5) Make sure to switch your site's theme over to your new theme on the admin/build/themes page.

The "content_top" and "content_bottom" matches the regions in the .info file. Very easy! Now you can ad any blocks you want above the content.

Comments