This article is the sequel to my first drupal adsense article. This time we're going to put adsense blocks in between teasers, on for example our frontpage. Same as last time, this does not require any modules, nor does it depend on adsense, it works with any phptemplate-based theme and with all sorts of ad code.

For starters, we're going to create a new block region. If there is not a template.php file in your theme folder, create one and open it up. See if there is a block regions function, if there is, add a new block to it: 'betwteasers' => t('between teasers')

If you didn't find a block region function copy this code into your template.php file (this code should be in between <?php and ?> tags):

function yourtheme_regions() {
return array(
'right' => t('right sidebar'),
'left' => t('left sidebar'),
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer'),
'subnav' => t('subnav'),
'betwteasers' => t('between teasers'),
);
}

Don't forget to substitute yourtheme with the name of your theme!

Now that we have a block region, we're going to need to place it in our page.tpl.php. Open that file, and look for the code ‘$content', this variable holds all the teasers on a frontpage, or a full content item on a node page. What we're going to do now is intercept $content on the frontpages, explode it into pieces (nodes), then put our adsense code in between some pieces, and lastly we're putting the whole thing together again.

I'll give the full piece of code first and then I'll explain how to fit it to your theme:

<?php
if (drupal_is_front_page()){
$arrtv = explode("</div>", $content);
$arrtv[3] = $betwteasers.$arrtv[3];
$arrtv[6] = $betwteasers.$arrtv[6];
$arrtv[9] = $betwteasers.$arrtv[9];
$content = implode("</div>", $arrtv);
print($content);
} else {
print($content);
}
?>

The first line should is obvious, it tests if we are on a front page. The second line separates all the div elements from each other and gives us an array. Now comes the tricky part, we need to figure out how many div tags represents a node. For my custom theme, there were 2 active div tags in node.tpl.php, so I added an ad block every 2 nodes. The code snippet above is used with Bluemarine, which has one more div tag in its node.tpl.php. If your theme is coded to modern web standards (aka semantically) you shouldn't have more then 2 or 3 div tags in your node template, so you would change the bolded numbers in the code snippet to either 2-2/4-4/6-6 or maybe 4-4/8-8/12-12.

If you are unlucky enough to use a badly coded theme you might have work to do still. If the theme uses a table layout you're going to have to look for another anchor point to stick your ad code to, maybe a </tr> tag. Garland, I noticed, is also coded badly, its coded suffers from what we like to call divilitus. That's what happens if you turn your code into a div soup. If your themes code is one big div soup you're going to have to play around with the numbers until it fits, or you're going can count the number of divs in node.tpl.php and substract the ones that you're are inactive in teaser view and in your specific website setup. With the latter I refer to how a theme might use an if-test to check if a piece off code needs to be placed:

    <?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>

This piece of code checks if you are using taxonomy and if you don't the taxonomy div element is not placed in your output.

Anyhows, the last step is to add a block with your ad code in it, and place it in the ‘between teasers' block region. This ad block will be repeated at every instance where you added the block region to a place in the array. If you would like to place different ad blocks, you would have to create multiple block regions and add a different region at every line of the code snippet for page.tpl.php

Drupal

5.x
0 Comments
Jan 4, 2008 By Jurriaan