How To: Insert Google AdSense Ads Between Posts on Your TypePad Blog
June 6, 2006
This article reprinted from the the TypePad Hacks Weblog. The original article can be found online:
http://www.typepadhacks.org/2006/06/how_to_insert_g.html
© 2008, John T Unger
UPDATE: Newer instructions that work with TypePad's updated code base can be found in the TypePad Knowledge Base here.
Asha Dornfest sent me an email a while back, asking if I would be interested in a tutorial on how to insert google AdSense ads between posts. Definitely! I've had several requests for this hack and since I don't actually have an AdSense account of my own, I was more than pleased to turn the job over to her.
She did a fantastic job! Her tutorial is clearly written and easy to follow, while maintaining a nice personal tone that makes it more entertaining and less intimidating than some tutorials. I'm totally impressed, and I hope that she continues to submit more tutorials in the future. Not many writers can balance clarity and conversation tone as well as she did. Asha, you rock and you can tell everybody I said so!
I've included the full tutorial here in the extended entry, exactly as Asha wrote it. I would also recommend dropping by her blog to read the full entry she posted. Before getting into the tutorial, she recounts some of her experiences with AdSense including lessons learned, and some good advice on placement of ads within your blog layout. Thanks, Asha, for granting me one-time rights to reproduce an unedited excerpt of the tutorial on TypePad Hacks.
Tags: adsense, blog, blogging, blogs, blogtools, design, google, advertising, typepad
Before you begin
- You need to be using TypePad Pro Advanced Templates so you can fiddle with TypePad tags. HTML know-how is helpful here, but not absolutely necessary if you can deal with brackets and are good at following directions.
- You also need to be a member of the Google AdSense program.
- You'll be doing plenty of cutting and pasting between Google and TypePad, so using a browser that supports tabs (such as Firefox) wouldn't hurt, either.
The hack in theory: Setting the lastn and offset attributes of the <MTEntries> tag
<MTEntries> is a container tag that represents the list of posts in your page. Whenever you see a list of posts in your blog, whether on the home page or an archive page, that list begins with the <MTEntries> opening tag and ends with </MTEntries> closing tag.
The key to inserting AdSense code (or anything else) between posts is to take advantage of two attributes of the <MTEntries> tag: lastn and offset. These attributes work together to control how many posts are visible inside the <MTEntries> container.
The value of lastn determines how many posts are visible. For example:
<MTEntries lastn="2">
...
</MTEntries>
specifies that this list contains the two most recent blog entries.
The offset value, when used together with lastn, enables you to change the starting point of your entry list. For example:
<MTEntries lastn="8" offset="2">
...
</MTEntries>
specifies that this list contains eight blog entries, but skips the first two entries and starts with the third.
Imagine, then, what happens when you combine the two sets of <MTEntries> containers with a bit of Google AdSense code. For example:
<MTEntries lastn="2">
...
</MTEntries>
[insert AdSense Code here]
<MTEntries lastn="8" offset="2">
...
</MTEntries>
creates a page that displays ten blog entries. Entries 1 and 2 appear at the top of the page, followed by a Google AdSense unit, followed by entries 3-10.
The <MTEntries> tag lives in the "entry-list" module included in the Main Index template. So, to change the <MTEntries> tag in your blog, you'll need to create a duplicate of the entry-list module. In the duplicate, you'll tinker with the <MTEntries> tag as I outline below, paste in your AdSense code, save the new module, and then swap out the default entry-list module for the new-and-improved module in your Main Index template. Easy! (Really.)
BIG, FAT CAVEAT: The only problem with using the lastn attribute - and it's a biggie - is that it restricts the display to entries posted within the last 30 days. If yours is a high-frequency blog, with new posts every day or two, you're fine. But if you only post once a week, which amounts to approximately four posts every 30 days, or if you want to include AdSense ads in your archive pages, your entry list won't display fully. I got around this on the Parent Hacks category archive pages by placing my AdSense unit at the top of the entry list, avoiding lastn altogether (here's an example).
Get the hack in theory? Time to apply it to your own blog.
Step 1: Decide on your layout.
Obviously, how you want your finished page to look will determine what size AdSense unit you'll use, and how you'll alter your blog's code.
My first attempt involved placing half-banners after every second blog entry. At the time, my home page contained ten entries, so I inserted a total of three half-banners, figuring that inserting more would result in diminishing returns as they'd be too far down the page to get many clicks.
I later changed the setup to a single medium rectangle after the first blog entry. The resulting code was much simpler, and the returns were similar to the first setup. In the interest of brevity, this tutorial highlights how to build the second setup (medium rectangle between first and second post), but the concept behind both layouts is the same; choose whichever suits you.
Step 2: Generate the AdSense code you'll eventually paste into your template.
1) Log into your AdSense account and generate your ad code.
I found that 234 x 60 half banners and 300 x 250 medium rectangles fit nicely into the body of the page. I recommend choosing a color scheme that blends with the text of your blog entries (a strategy most other bloggers would agree works well).
2) Copy the AdSense code and paste it into a sticky note or plain text file so you can grab it later.
Step 3: Copy the original code for the entry-list module from the TypePad documentation.
1) Open a second tab in your browser (or a new window if you're not using a tabbed browser) and go to the Advanced Templates: Template Module page in the TypePad Knowledge Base.
This page contains all the original code for the default template modules.
2) Select the contents of the entry-list box and press Command+ C (or Ctrl + C) to copy the code into your clipboard memory.
Step 4: Create a custom entry-list module.
1) Open a third browser tab and log into your TypePad account. Access your blog's design template.
[Paranoid aside: if, like me, you're nervous about working on your live template, I strongly suggest creating a copy of your current design, fiddling with the copy, and then applying the design to your blog only when you're sure it works properly. As hacks go, this one is pretty low-risk as you're working with modules, most of which have backup copies in the TypePad documentation, but still, it's a good habit to develop.]
2) In the Edit Current Design tab, click the "Create new template module" link.
3) In the Create New Template tab that appears, name the new module "entry-list-main."
You want to name the module something other than "entry-list," as the original entry-list module also appears in the Category Archive and Date-based Archive templates. The module you're creating will only appear in the Main Index Template, hence the unique name.
4) Click inside the Template Body box and then press Command + V (Ctrl + V) to paste the original entry-list code (it's still sitting in your clipboard memory, remember?).
5) Hit the Enter key a few times to insert a bit of white space beneath the code.
This is simply a helpful visual cue -- you'll eventually paste your AdSense code into this white space).
6) Now press Command + V (Ctrl + V) again to paste a second copy of the original entry-list code directly beneath the first set.
The module should now contain two identical sets of <MTEntries> containers.
7) Click the Save button in TypePad. (I'm a compulsive saver. Humor me.)
Step 5: Alter the <MTEntries> tags in the custom entry-list-main module
1) In the first <MTEntries> tag, set the value of the lastn attribute.
This is the number of blog entries you want to appear before your AdSense unit. In Parent Hacks, I display a single post before my AdSense unit, so my first <MTEntries> tag looks like this:
<MTEntries lastn="1">
...
</MTEntries>
(In English: show only the first entry)
2) In the second <MTEntries> tag, set the values of both lastn and offset.
In this case, the value of lastn is the number of blog entries you want to appear after the AdSense unit. The value of offset should take into account the lastn value of the first <MTEntries> tag. In Parent Hacks, I wanted the home page to contain a total of ten entries. Because the first entry was already visible thanks to the first <MTEntries> container, the second <MTEntries> tag looks like this:
<MTEntries lastn="9" offset="1">
...
</MTEntries>
(In English: show the next nine entries, starting with the second entry)
If, instead of a single AdSense unit between the first and second post, you want to insert AdSense after say, every second post, your entry-list-main module would look something like this:
<MTEntries lastn="2">
...
</MTEntries>
<MTEntries lastn="2" offset="2">
...
</MTEntries>
<MTEntries lastn="2" offset="4">
...
</MTEntries>
<MTEntries lastn="2" offset="6">
...
</MTEntries>
<MTEntries lastn="2" offset="8">
...
</MTEntries>
Step 6: Insert AdSense code between the <MTEntries> containers
1) Copy and paste the AdSense code you generated in Step 1 into the white space between the <MTEntries> containers.
If you want to display a single AdSense unit (as I do on the Parent Hacks home page), you'll only need to do this once.
<MTEntries lastn="1">
...
</MTEntries>
[AdSense code goes here]
<MTEntries lastn="9" offset="1">
...
</MTEntries>
Otherwise, paste the code between each set of <MTEntries> containers.
<MTEntries lastn="2">
...
</MTEntries>
[AdSense code goes here]
<MTEntries lastn="2" offset="2">
...
</MTEntries>
[AdSense code goes here]
<MTEntries lastn="2" offset="4">
...
</MTEntries>
[AdSense code goes here]
<MTEntries lastn="2" offset="6">
...
</MTEntries>
[AdSense code goes here]
<MTEntries lastn="2" offset="8">
...
</MTEntries>
2) Optional: I find the ad units look nicer with a little padding underneath, so I surround each bit of AdSense code with a <div style="padding-bottom: 20px"></div>.
3) Save!
Step 7: Include the new entry-list-main module in the Main Index template.
1) In the Select Template To Edit dropdown list, select Main Index Template, and then click the Go button.
2) In the template body, scroll down to about the middle of the page until you see the following tag:
<$MTWeblogIncludeModule module="entry-list"$>
3) Change the tag as follows:
<$MTInclude module="entry-list-main"$>
That's it! Now save and publish the template (you can publish index pages only to save time), and behold your site in all its revenue-earning glory.
More Like This: Advanced Templates , Blog Tools , Hacks for Layout + Design





Jennifer Wilde says:
I want to have an ad show up every third post. But it seems like too much of a hack to do multiple calls like this:
...
...
...
especially since then I'm hard coding how many entries show up. I would like to use Javascript to set a variable then check for mod 3 to see whether or not to display the ad. But so far, I can't get the script to work. Nothing I put within the tags seems to work. I tried using an alert, a document write, a conditional statement...
Are there limitations to how Javascript can be used? If I can get this working, I would be more than happy to share my solution.
thanks!
Posted: Jul 5, 2007 5:14:45 PM
Fox Tucker says:
I spoke with TypePad support on Wednesday (27 Jult '07) and was told that they are considering removing the limitation of the "lastn" attribute, but there was no clear indication of when this may happen.
I'm not sure if it's a big enough issue (to TypePad) for them to shout about should they ever get round to it.
Posted: Jul 27, 2007 10:16:07 AM
Preston says:
FINALLY! I Got it! I haven't been playing with the custom modules much yet, so the thing that really got me hung up was steps 2 & 3 at the very end. When you go into the main index template to place your new custom module name in the right place, you have to change the other stuff too!!
From:
$MTWeblogIncludeModule module=
to:
$MTInclude module="xxxxx"
This was hanging me up the whole time, I just didn't catch this!
Posted: Aug 9, 2007 1:43:32 AM
Lesli says:
When I do this, my main page goes blank. I read you post that sometimes this is a missed closed html tag. Well, for 2 days now I have reviewed my code but i don't see anything is wrong. As soon as I change the Main Index Template from <$MTWeblogIncludeModule module="entry-list-sticky"$> to <$MTIncludeModule module="entry-list1"$> (entry-list1 is the name of my new template), the world goes blank.
I know you are bombarded, but could you please offer some assistance? I am pregnant, this isn't working, the hormones are raging, and this could be a life-or-death situation :) Thanks.
Posted: Mar 8, 2008 1:53:46 PM
John T Unger says:
Hi Lesli,
You wrote:
Your template syntax is incorrect.
<$MTIncludeModule module="entry-list1"$> needs to be written as
<$MTInclude module="entry-list1"$>
See the difference? If you fix that tag things should be fine.
Posted: Mar 11, 2008 12:37:41 PM
Yeti says:
John,
Hoping you could help me out on this. I followed the directions EXACTLY as indicated and the ads were placed perfectly. HOWEVER...now my "pagination" older posts / newer posts feature at the footer of each page has mysteriously disappeared! Not sure if it has anything to do with the fact that I had no actual:
<$MTWeblogIncludeModule module="entry-list"$> tag in my Main Index template. Instead the only thing remotely close was <$MTWeblogIncludeModule module="entry-list-sticky"$>.
So I replaced that instead. Do you think that might be why the footers disappeared? Would you happen to know any resolve? Any help would be greatly appreciated!
Thanks!!
Posted: Mar 24, 2008 4:39:10 AM
Charles says:
Hey John:
Quick question: Is there any way to implement this on a "Category" page?
So far, I've had no luck as there seems to be no way to define the "lastn" of a specific category...
Posted: May 22, 2008 4:56:49 PM
Mike says:
These were great instructions. One question, what happens if you have a blog set as your feature blog. I currently have a welcome message set as my feature blog so it will always appear first. When I implent your instructions, it discounts my feature blog. How can I implement what you have provided and still have a featured blog?
Thanks,
Mike
iBlogGolf.com
Posted: Jun 12, 2008 12:56:08 AM
TypePad Engineering says:
Hi John,
Since we added the pagination functionality to blogs recently, this hack no longer works quite right. Instead, you can use the MTEntryIfOrderNum tag to determine where you are in an MTEntries loop and display additional content between entries. Here's an example:
<MTEntries>
<$MTEntryTitle$>
Add any other MTEntry tags and HTML code as you wish.
<MTEntryIfOrderNum num="0">
This will be displayed following the first entry.
</MTEntryIfOrderNum>
<MTEntryIfOrderNum num="3">
This will be displayed between the fourth and fifth entries.
</MTEntryIfOrderNum>
<MTEntryIfOrderNum num="6">
This will be displayed between the seventh and eighth entries.
</MTEntryIfOrderNum>
</MTEntries>
The entries shown on a given page will be numbered starting at 0, starting from the top and going downwards on the page. Instead of many MTEntries blocks, you'll have just a single MTEntries with multiple MTEntryIfOrderNum blocks in it. As TypePad generates the page, it will look at each MTEntryIfOrderNum block, and when the "num" parameter matches the current position on the page, TypePad will process the code and text inside that MTEntryIfOrderNum block. The process repeats until TypePad has finished looping through the MTEntries block.
Finally, you'll need to add pagination links, which are explained in this KB article:
http://kb.typepad.com/id/1252
Posted: Oct 24, 2008 7:10:41 PM
TypepPad says:
TypePad Knowledge base article that includes the information from the previous comment:
http://kb.typepad.com/id/876 -- Tip: Displaying Content Between Posts in an Advanced Template
Thanks again from TypePad!
Posted: Oct 24, 2008 8:06:46 PM
jeff paul says:
well i must say its a great source for those who are looking for the easiest way for making money on the internet and thats Google adsense and typepad are always a great source for increasing traffic .
Posted: Dec 19, 2008 1:21:18 AM