Wordpress Hack – How to Insert Ads in the Middle of Your Content!
The other day I showed you my latest Adsense heat map that I have been working on for 3+ years. Mind you, that’s the only way I can give back to the community of bloggers. It’s not going to help me in the least bit because I will have more competition but I feel that it’s important to give back. Karma I call it.
Anyways, here’s how to insert ads after 1-3 sentences after your content. (Or you can change the $tcount>=1 values to something like $tcount>=3 if you want to show ads after more sentences.)
You can even show additional ads at the bottom of your content if your blog post is a long blog post versus short.
Here’s the code that will let you insert ads at different places in your content.
You need to replace in your single.php file (usually) with the following code:
The above code will print your first Adsense block after the first HTML tag p, which is usually a paragraph block or 1-3 sentences.
There’s also an option to print a second Adsense block after 4 ps for longer blog posts. This ad block won’t show unless your blog post is super long. I don’t really use the second Adsense block much. You can also change the $tcount>=4 to something like $count>=2 if you want your second Adsense block to show after the 2nd
block.
There’s also many ways to customize this code such as detecting images. For example, I used additional code to detect whether there’s an image in the very beginning of the post. If that’s the case, I will want to show the ads under the image because I think it looks better as far as web design/usability goes. I can have that code up too if more of you guys want it.
Here’s some additional div tips for styling your Adsense ads.
If you want to wrap to the right for 336×280 ads:
Replace this line with Adsense code here
If you want to wrap to the left for 336×280 ads:
Replace this line with Adsense code here
If you want to just display ads without wrap, with Adsense left aligned:
Replace this line with Adsense code here
If you want to just display ads without wrap, with Adsense center aligned:
Replace this line with Adsense code here
In general, I find that wrapping your Adsense around text can be good for CTR plus look great with your overall site design.
At other times, I find that Adsense aligned-left with no text wrapping (with white space on the right) gets a lot better CTR.
Just test the different locations to see what works best for your niche.
Instead of putting the ad after the content, it’s simply common sense they will perform better if you put it within the context of your content and higher up on your blog post.
The amount of CTR increase skyrockets plus why not?
Of course, just make sure the overall site design and user experience are still maintained, no one wants to read ad-plastered blogs, even though I am on the very edge.
Enjoy and don’t forget to share the code with other Wordpress bloggers out there!










March 4th, 2010 at 6:13 pm
[...] Now, after the first Adsense rectangle block, I have content, perhaps 1-3 sentences. I have used custom code for my Wordpress theme to put another Adsense rectangle block after the 1-3 sentences. [...]
March 4th, 2010 at 11:39 pm
Thanks Max, this is really good stuff!
March 5th, 2010 at 5:14 am
Hey sure, I am happy if it helps someone!
March 5th, 2010 at 11:29 pm
Hello, code is beautiful and it works but in a real environment, code breaks the html content. I see that this code delete the
tag and your paragraph will be without an end, this means pages aren’t anymore w3c ok, although they were before. Do you take a look at this, if possibile ? By the way thanks for sharing this really bautiful idea.March 5th, 2010 at 11:30 pm
Previous comment was broken, i was speaking about the end of the p tag.
March 6th, 2010 at 12:31 am
Problem solved, i did some testing and, adding 2 echo do the trick. You find the new code at:
http://pastebin.com/KfsQA1ig
But we still have a (little) problem. w3c markup validation is ok but next-page wp tag is broken with this solution
March 6th, 2010 at 7:31 pm
Great thanks for the valid code!
March 13th, 2010 at 5:02 am
thanks for the tutorial. i will try it now.
July 25th, 2010 at 3:15 am
note, this code break html tag after all content, there are without start