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/10/06 @ 12:11
If you're interested in a cool method for filtering the ads for certain domains, check out deekayen's tip. He just adds another part to his if statement:
&& !preg_match(PUT IP ADDRESS TO MATCH HERE, $_SERVER['REMOTE_ADDR']))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
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?
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.
03/28/06 @ 16:19
Thanks! I suddenly needed to do just this; I'm glad I bookmarked your tip!
01/20/08 @ 07:43
Is there anything you can't do??? Great stuff - thanks
02/14/08 @ 00:05
Good tips. I just swicthed over from wordpress so this was very helpful!
02/17/08 @ 10:25
Another great tips. Thanks :)
03/16/08 @ 13:10
Thank you very much! It's a very useful tip =)
Post new comment