Adsense Tweaks
August 22, 2005 on 9:54 pm | In 31 Days to a Better Blog, AdSense, Blog Housekeeping, Blogging |I have been mentioning for a few days that I have been spending some time Tweaking the AdSense that appears here and on my Nicolas Darvas Website.
One of the biggest tasks was placing an 250 x 250 AdSense Box that appeared in just the first post of a displayed web page. If anyone is inerested in doing this themselves, then here is a brief description of how I did it.
The most important thing was to hack the index.php file in the current theme. What I needed to achieve was to insert the AdSense box into only the first post and not any of the subsequent ones.
So, to achieve this, I had to put a counter variable at the begining of the index.php file - I hope it doesn’t get too geeky!:
< ?php $showAdSense = True; ?>
Next, I had to scroll down through index.php to find the start of the <div> tag which handles the style for each post - In my current Theme, it is:
<div class=”entry”>
Once I had found this, I pasted my AdSense code immediately before it - with some PHP and a style wrapped around it - The PHP made sure only one block would be displayed and the style would place it in the right area of the post. Here is the code:
<?php If ($showAdSense) { ?>
<div style=”float: right; margin: 0px; margin-top: 10px; padding-right: 0px; padding-left: 5px”>
<script type=”text/javascript”><!–
google_ad_client = “pub-XXXXXXXXXXXXXXXX”;
google_ad_width = 250;
google_ad_height = 250;
google_ad_format = “250×250_as”;
google_ad_type = “text”;
google_ad_channel =”";
google_color_border = “FFFFFF”;
google_color_bg = “FFFFFF”;
google_color_link = “CC6600″;
google_color_url = “6699CC”;
google_color_text = “000000″;
//–></script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
</div>
<?php $showAdSense = False;} ?>
Basically, the first line of this code asks is there an AdSense Box here?. Because the showAdSense variable is set to True - then the answer to this question is Yes - so, the AdSense code is inserted into the HTML for the page.
At the end of the AdSense insertion, there is a piece of PHP code that then sets the showAdSense variable to False - so that the when it comes to insert the next post and the page asks if there is an AdSense box - the answer will be No.
And that - believe it or not - is all there is to it. Of course don’t be fooled by how easy it is - Remember to ALWAYS back-up any files you are working on when Hacking your Blogging Software. I know from bitter experience the pain of trying to unpick crappy code because you can’t restore to the original version.
Oh - and I’ve inserted all those XXXXXXXX’s in the AdSense block where my AdSense ID goes. If you would really like to show my ads on your pages - please let me know as I’ll be only to happy to oblige!
Related Posts:
3.28767 is the first Google AdSense Magic Number
Michael Cheneys AdSense Videos (Update)
Michael Cheneys AdSense Videos
AdSenseLog - Desktop AdSense Account Application
Chitika eMiniMalls
No Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^








