Drupal tip #2: Sprinkling ads throughout your blog

Today's tip is simple, but potentially very useful to someone. One of the things you might notice if you visit some of the popular commercial blogs is that they sprinkle ads throughout their list of blog entries. For example, on your blog page, you might have a row of ads after the 2nd and 6th blog entries or something. Tweaking your ad placement is one sure way to increase your click-throughs. Doing this in Drupal is easy.

In your PHP Template theme, the node.tpl.php file controls the display of your nodes listing (e.g. node/ and blog/ pages). If we wanted to put an ad after the 2nd and 6th blog entries, we can put this code at the top of that file:

<?php if ( !$page && ($id == 2 || $id == 6)) : ?>
[replace this comment with your ad code]
<?php endif; ?>

The first part of the if statement (!$page) checks to make sure we're not looking at a single node page, e.g. node/view/1. The second part ($seqid = = 2 || $seqid = = 6) says to apply this code only to the 2nd and 6th entries on the page. Just change the numbers to wherever you want the ads to appear. Then drop your Ad code (e.g. from Google Adsense) in to replace the comment. To see what this looks like, check out the Weblog page on this site.

Note: According to http://drupal.org/node/46209 $seqid becomes $id in 4.7

Comments

01 jibbajabba
01/10/06 @ 12:11
02 deekayen@drupal.org
01/31/06 @ 00:52

I think continued support of $seqid just depends on making a patch to close this bug: http://drupal.org/node/45047

Advertisement
03 Anonymous
02/21/06 @ 05:49

I tried adding this into my theme's node.tpl.php file, but it had no effect...
Does this work in Drupal 4.7?

04 jibbajabba
02/21/06 @ 11:54

As noted in the entry, you have to use $id rather than $seqid in 4.7, but I haven't tested this yet. I'm only running 4.6.x on this site. Check the PHP Template threads and documentation on Drupal.org for updates.

05 Adam Gaffin
03/28/06 @ 16:19

Thanks! I suddenly needed to do just this; I'm glad I bookmarked your tip!

06 Charity cds
01/20/08 @ 07:43

Is there anything you can't do??? Great stuff - thanks

07 buy beats
02/14/08 @ 00:05

Good tips. I just swicthed over from wordpress so this was very helpful!

08 Job Profiler
02/17/08 @ 10:25

Another great tips. Thanks :)

09 Brownie
03/16/08 @ 13:10

Thank you very much! It's a very useful tip =)

10 beats
12/09/08 @ 23:30

Nice tip for increasing ad click thrus - thanks!

Advertisement
11 Anonymous
12/10/08 @ 08:28

Sweet. I wondered how Drupal sites did this. This is exactly what I've been looking for.

12 Anonymous
06/06/09 @ 16:23

after lots of searches seeing this easy way was quite helpful.

thank you.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <strong> <dd> <dl> <dt> <i> <li> <ol> <u> <ul> <code> <blockquote> <fieldset> <legend> <h6> <span> <img> <div> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options