Conditional Display of AdSense in WordPress
My initial requirement was to be able to display Google Ads in the content and in the sidebar too.
I installed AdSense-Deluxe thinking that it will take care of both of my requirements. The plugin does a very good job of displaying ads but it
displays ads inside content only. I wanted to be able to display ads in sidebar too! Instead of searching for another plugin which can do that, I wrote a small script in my theme’s ‘functions.php‘ and it was done! I never realized that it was so easy to do that!
Now, I can display a 160×600 wide skyscrapper (as Google calls it) if visitor is on homepage and will display a 120×240 vertical banner if a page with single post is viewed. Since the length of the main page is too long, I display the ads three times so they look as if they are continuation of the sidebar.
Conditional Display of AdSense in your site?
If you are interested, and want to set it up on your site, here is the procedure.
Step1: Before anything, backup your functions.php and sidebar.php from the themes folder. Download my Conditional AdSense Code (documented) to use it with modifications. Then, in the WP Admin Panel, goto the Theme Editor and select ‘Functions.php’ on the right hand side for editing.
Step2: When functions.php is open for editing, scroll towards the end of code block and just before the PHP closing tag (looks like ?> ), insert the code (did you download the code?).
Step3: I assume you are done with all the above changes and did not encounter any error messages when you saved the functions.php file. Now open sidebar.php file and see the last line. You should see a closing DIV tag. Just before the DIV tag, insert the following line:
< ?php disp_google_ads() ?> //Remove the space in between first two characters.
Save your sidebar.php and check your homepage and also your sidebar.
Note: Depending on your theme, you may or may not find the closing DIV tag in the last line of sidebar.php, but you can try to place the disp_google_ads() function in the same file anywhere, save it and check your page for the output. Make changes till you achieve the result. Should not be very difficult.
In case of any issues or suggestions, please drop in your comments.
Good luck! ![]()

aNieto2k Said,
November 8, 2006 @ 6:21 am
Is more easy than your code.
— code (php) —
if (is_home()) {
— home ads code —
} else {
—- single ads code —
}
— /code —
Also you can use is_single() for do the same.
— code (php) —
if (is_single()) {
— single ads code —
} else if (is_home()){
—- home ads code —
} else {
— Other posibles pages ads —-
}
— /code —
Raj Said,
November 8, 2006 @ 11:17 am
Hello aNieto2k,
Thank you for your feedback. I am fairly new to WordPress (9 days in WP as of today). I did not knew about is_single() tag of WP. I will certainly look forward to rewrite the code
Thank you!
Jaylon Said,
November 28, 2006 @ 2:45 pm
Hi, I have found a few wordpress - made for adsense templates. I am looking for one that is more “successful”, even if its not made for wordpress (I can convert it).
Can someone point me into that direction. Trying to speed up my contextual sites.
here are what I found (if one you recommend is on this list, just tell me.).
http://www.sapiensbryan.com/index.ph…me-ads-minded/
http://pilkster.com/wordpress-themes-for-adsense/
Anything else?
I also can take a look at sights that have successful ctr adsense templates (But unless you want it public, please PM me it.)
OK I Just realized I mispelled Wordpress in the title, Man I am an Idiot.!
Ok now I am a bigger idiot for not realizing the “go to advance” edit. so i fixed my misspelling.
Raj Said,
November 28, 2006 @ 11:57 pm
Jaylon,
I have sent you an email with information you needed.
Coyote Said,
February 9, 2007 @ 2:15 pm
Hi there,
I’ve download and edited your code but I need help with the logic please. Basically I don’t want any ads to appear on my home page (e.g. /index.php) for each article, however when someone clicks into an article/post I do want the ad displayed.
What is the conditional code/logic I need to apply here in the functions.php file please? Thanks.
btw. The site where I’m testing this new code is http://www.avalon5.com/wptest
I’m trying to break in a new theme at the same time
c0y0te
Raj Said,
February 9, 2007 @ 2:56 pm
Hi Coyote/Rob,
I have sent you an email with updated code. Give it a try and lemme know!
Alex Said,
August 9, 2007 @ 11:45 pm
Hi Raj
Since you bloghash I liked it…
I read about your post called: Conditional Display of AdSense in Wordpress and
since Google’s AdSense program is so strict and their terms.
In your instructions to setup AdSense at the sidebar your instruction says that:
I will have to put a \ (backslash) in front of every quote (”)…
My question is if this action: put a \ (backslash) doesn’t violate the Google’s terms?
Thanks a lot for any help to solve my doubt. and congratulations for your blog.
Raj Said,
August 10, 2007 @ 5:38 am
Hello Alex,
Appreciate your question on using those backslahes. We actually use the backslash as an escape sequence kind of thing to properly format the output. Since we do not modify the AdSense code when it’s finally produced into the HTML page, we thereby do not violate their terms and conditions.
Thank you visiting my blog and asking this question
Alex Said,
August 11, 2007 @ 9:16 pm
Hi RAJ
Thanks to you for your super fast answer!
Really great!
I did that question since that Google’s terms are very strict and
any time could… Google says… You’re fired!
I’ll try to do your procedure to install the AdSense on my
sidebar of my Wordpress blog.
And again thanks for your answer, I hope that work fine.
RAJ congratulations you have a good blog
Alex Said,
August 18, 2007 @ 11:52 am
Hi RAJ
About my question above (before post) I’ve the last question…
Why you don’t have currently in your blog’s sidebar an AdSense block?
OK. that’s all, have a good time and thanks a lot for your answer.
Raj Said,
August 19, 2007 @ 3:53 am
Alex,
In the past I had a 160×600 ad unit on the right of the post but that did not perform well. After experimenting with ad units at different places, I found the current placement to be best performing so far.