« Dynamic Publishing - Pros and Cons | Main | Simple RSS Customizations »

Google Adsense Placement After First Post

Google has 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 section on Displaying Entries.

Normally on your Main Index template, you would have your MTEntries section laid out like so (MT3.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">


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.


Comments (19)

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.

Marza Author Profile Page:

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.

I found that this tip when used in conjunction with Alden Bates' plugin paged archives produced incorrect index pages - they were all the front page.

However, if I placed the google script between:


I got the desired effect.

This trick work in Movable Type 4? In my case with mi site don't work, only appears after first post.... I need to put it all post and not only bottom the firts post.

Hi xavierx -

If you want to put it in every post, you don't need to mess with lastn or offset. Just put it inside the <MTEntries> container:

  <!-- adsense code -->

And you're set. Just keep in mind that you're only allowed to have three AdSense blocks per page (as of this writing), not to mention that if you had more, you may get lesser-performing ads anyway - so you may want to use lastn and offset.

I tried putting the google adsense code in the entry summary template module and inbetween the in the index template in MT 4.

The ads appear after the first three entries and then I get ugly error messages in the next three entries that state "The Web page cannot be found." They are really ugly in Internet Explorer.

Does any one have a sample index template I can look at with the variables all set up to place google adsense after the first three entries only?

I've read this article several times and I still get a little lost.

You can e-mail it to me directly at lwriter@charter.net

Hi Lynn -

You might want to check out the sidebar tutorial that was written recently, as it does a great job of going over how the modules work with one another.

Aside from that, I'd probably suggest not using the Entry Summary template module, because then you're going to get the code in every one of them - instead, you should probably put it outside of the module - for instance, right after it is called, perhaps in the Entry Listing archive template - so that you can better control how many of them appear on your page (since you're only allowed to have three on a page).

Post a comment

(If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)