June 02, 2005

Google Adsense Placement After First Post


Google recently released some useful tips on how to optimize the use of Google Adsense on one's web pages. No surprise - the hottest "hot spot" for the best click-through returns lies right in the middle of your content.

How does one place a Google Adsense ad (or any other ad, bit of text, or image for that matter) between entries on the main index page of your MT weblog? By using a simple trick of the "lastn" and "offset" attributes outlined in the MT manual here.

Normally on your Main Index template, you would have your MTEntries section laid out like so:

for MT 3.2:


<MTDateHeader><h2 class="date-header"><$MTEntryDate format="%x"$></h2></MTDateHeader>
<a id="a<$MTEntryID pad="1"$>"></a>
<div class="entry" id="entry-<$MTEntryID$>">
<h3 class="entry-header"><$MTEntryTitle$></h3>
<div class="entry-content">
<div class="entry-body">


for MT 3.1:


<h2><$MTEntryDate format="%x"$></h2>

<h3 id="a<$MTEntryID pad="1"$>"><$MTEntryTitle$></h3>


To put a Google Adsense ad after the first entry shown (the most recent entry) and before the second entry, make a copy of all of your <MTEntries> code and paste it right after the first chunk of code. Add the attribute lastn="1" to the first <MTEntries> tag like so:

<MTEntries lastn="1">

For the second block of MTEntries code, add the attribute lastn="x" where x equals the number of entries you would like to show on your main blog index page, minus one, and add the attribute offset="1". The tag for the second set of MTEntries code would then look like this:

<MTEntries lastn="6" offset="1">

Save and rebuild your Main Index template before moving on to the next step to make sure it's working.

Next, add your Google Adsense code between the two blocks of MTEntries code like so:

<MTEntries lastn="1">

<script type="text/javascript"><!--
[Google Adsense code]

<MTEntries lastn="6" offset="1">

Save and rebuild your template.

How to add Google Adsense to RSS feeds - an explanation by Dave Taylor

Has this tutorial been helpful? Please consider linking to Learning Movable Type at http://learningmovabletype.com/ . Thanks!


If you would like to send a trackback
please use the following URL: http://learningmovabletype.com/cgi-bin/mt32/mt-tb.cgi/432


One could alternatively use the MTEntriesHeader tag which basically does the same thing :)

From the MT manual:

MTEntriesHeader A container tag: the contents of this container will be displayed only if this entry is the first entry listed in the <MTEntries> container.

So I guess if you wrapped your Google code within MTEntriesHeader opening and closing tags, right before the closing tag of you MTEntries section, that would do the trick.

I've been using this method succesfully, but found it has one minor setback: when you display dates for each day that is listed on your Main index, and there are multiple entries per day, this date will be repeated under the ad.

I wonder if anyone has found a solution to display the date under the ad only if it is not the same date as the one above the ad.

Well if you use MTEntriesHeader and then wrap MTDateHeader around the MTEntryDate tag then the date will be displayed for every new day of posts and the adsense code will be displayed after the first ad.

As always, great job Elise. I was wondering though if you have given any thought to writing an entry on how to incorporate advertisments at the begining or end of individual entries and category archives. Perhaps you have already done so and I missed it.

Thanks for the tip. It seems as though my ads are always the same. Is this something that will get fixed over time or is there something i need to do on myside. Thanks raff


I have a similar problem. Let me know if you find a solution and I will do likewise.

Hi Jeff,
Incorporating an ad at the beginning or at the end of an entry is easy. Just put the Google code where you want it to appear in the Individual entry archive template. Same for the category archive template.

Akaraff - If you are getting the same ads, it's because those same advertisers are the ones that are willing to bid the most money for a spot on pages with your keywords.

Got a question: would changing the lastn value to 4 instead of 1 put the google ad after the 4th post (similar to treehugger.com or gizmodo.com)?


Thanks a lot for the hint Elise.
I have one more question...
What if I want AdSense or any other content to appear, let's say, every 5 posts? I can't manage to do it right :-(

Grazie :-)


Use <MTEntries lastn="5"> and <MTEntries lastn="5" offset="5">

That will put an ad after the fifth entry. But not after every fifth entry. Haven't thought about how to do that. But you really shouldn't have more than 10 entries on your main index anyway. The page starts to get heavy and takes too long to load.

Thx for the great tips! Does someone know how to do the same for the monthly archives?

Elise, do you know how to do that but within comments?

I mean, i have few entries with more than 100 comments, and id like to put adsense every 50 or 75 comments.. it is just an idea but i still didnt get how to do that.


Mariano - I have no idea. You might try asking over at the MT Support Forums.

Post a comment

(Before posting a comment please see the Comments and Trackbacks Policy. Do you need help troubleshooting your weblog? Please post questions and requests for support at the MT Support Forums. Thanks!)

Remember Me?

(you may use HTML tags for style)

Email to a friend

Email this article to:

Your email address:

Message (optional):