Share/Bookmark

Show More Adsense Ads on Long Posts

I'm old school. On my static sites, I hand code the side nav bar I want to use. This enables me to use different side nav bars for different situations. For instance, I may want to use a home side nav bar for my home page, side bar Z for section Z, and side bar 8 for yet another section of my site.

Now that I'm using WordPress to run some of my sites, I've pretty much stuck with one side bar for all my pages. Sure I could put in a bunch of if statements to pull in different nav bars, but that could get really messy fast. So, I've come up with two methods that I could use to alter the side nav bar and the layout for single posts and pages. This will come in handle for showing more ads on a long page and disabling ads for a short page.

Method One

This method will show less content or ads for single posts and pages (ie – about page) on the side nav bar. First, layout the side bar (called "sidebar.php" in most templates) content the way you want it for single posts and pages. Then add the following code in the side nav bar for your extra content or ads:

< ?php if ((! is_single()) and (! is_page())) { ?>
          <!– place your additional content or google adsense code here –>
< ?php } ?>

If you want to disable the ad for just "pages" then use:

< ?php if (! is_page()) { ?>
          <!– place your additional content or google adsense code here –>
< ?php } ?>

You can also go the other way and show more content or ads for single posts:

< ?php if (is_single()) { ?>
          <!– place your additional content or google adsense code here –>
< ?php } ?>

This first method will show the same side nav bar for all single posts and/or pages. But what if you want to show more or less ads for long or short posts. For that, you will need to use method two. If you can insert Google Adsense code in your template, you shouldn't have any trouble with method one. The second method is just a little more work and involves custom tags.

Method Two

This method will allow you to enable to disable content or ads base on a WordPress custom tag. This means you can have a short post display one ad and have a long post display two ads. First, go into the WordPress "Edit Post" page (admin menu) for a short post you created. Near the bottom of the screen is an area called "Custom Field" — located above the preview frame. Type in "skip" (w/o the quotes) in the "key" text box. Then enter "true" in the "value" text box. Click the "add custom field" button and WordPress will save the info for you. Next time you want to skip an ad, you just select "key" from the drop-down menu and type in "true" in the value text box.

Now, go edit single.php and add the following code around the content or ad you want to disable.

< ?php $skipad = false;
$skipad = get_post_custom_values("skip");
if (! $skipad) { ?>
          <!– this area contains the content or ad you want to disable –>
< ?php } ?>

If you have another ad you want to disable, you can omit the first two lines (you still need the php tag):

< ?php if (! $skipad) { ?>
          <!– this area contains the content or ad you want to disable –>
< ?php } ?>

This entry was posted in Adsense, How-to, WordPress. Bookmark the permalink.

One Response to Show More Adsense Ads on Long Posts

  1. Pingback: Kevan Gilbert Online » Blog Archive » Extensions and Plug-ins That Should Be Created But Haven’t Been Yet

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

iZachy Newsletter

Don't have time to visit iZachy everyday? Then sign up for my free newsletter. I'll send you an email when I have something to share with you. Your email address will be kept confidential and I will not share, sell, or rent it to anyone. You can unsubscribe at any time by clicking a link in the email.

Enter your email address to sign up for my newsletter:
  

Or you can also sign up for our blog feed.