So Very Posh

Thanks for visiting! This is the weblog of Lynda, a 26 year old girl living in Atlanta with her husband, three cats and two cockatiels.


Ads Between Entries


Surprisingly (to me at least) I get a lot of questions about how I insert Google Ads between my blog posts.  I started doing it with Movable Type, so this method can probably be applied to just about any blog format allowing PHP and also can be applied to anything, not just a google ad.  For purposes of this tiny little tutorial, I’ll tell you how to do it in EE.

First, I put my Google Adsense javascript into a separate template.  This isn’t necessary of course, but if I ever need to change the colors or anything, it makes it easier to edit rather than having to go back to each page it’s added under. 

Next, the page displaying the Adsense ad will need to be set to PHP Output.  I’ve been told it doesn’t work with PHP Input.  I frankly don’t understand why anyone uses input since it seems so horribly limited in what it can accomplish, but I guess that’s a rant for another day.

Now it’s just a matter of inserting the code. 

BEFORE the {exp:weblog:entries} tag I place the following:

<?php $googlead = "0"; ?>

Directly after the {exp:weblog:entries} tag, I place this:

<?php if ($googlead == 2) { ?>
{embed="template_group/adsense_template"}
<?php } ?>

Change the number “2” to whatever entry you want the ad to appear under.

Finally, in order to get this to work, directly before the closing {/exp:weblog:entries} tag, place this:

<?php $googlead++; ?>

So the finished product will look something like this:

<?php $googlead = 0; ?>
{exp:weblog:entries}

<?php if ($googlead == 2) { ?>
{embed="styles/inc_google"}
<?php } ?>

-- REGULAR WEBLOG CODE HERE --

<?php $googlead++; ?>
{/exp:weblog:entries}



Comments

Comments are closed for this entry

Thank you for sharing this with me a while back.  It’s easy to install and works beautifully to insert most anything (not just adsense).  Now if BlogAds would just create horizontal strips.

Posted by Doc on 07/22 at 09:49 PM | Link
Trackback: Catching Up with Bookmarks
WordPress Auto-close comments Recent commenters and commented posts; additional plugin available here Burningbird's WP modifications -- includes tons of helpful tips ExpressionEngine Inserting ads between entries Banning partial IP numbers Movable Type Backing up your blog Miscellaneous Display your Google page rank
Tracked on: Code Novice on 07/23 at 02:50 AM

The main reason to use PHP Input is to allow you to create “edit this entry” links.

Posted by Etan on 07/30 at 09:09 AM | Link

hi Lynda

I am trying out this snippet- “Ads Between Entries” but can’t seem to get it work 100%. See my home page for what I mean. The Google ad appears 3 times though I am trying to place it under story 2 only.

Thanks
Steve

Posted by Steve on 08/06 at 04:33 AM | Link

Thanks for your solution to this problem. However, I’m not sure how to set a page to php output. Could you please elaborate for a novice?

Posted by John Retzer on 12/22 at 05:52 PM | Link

It works! smile But.  :( I get the following error on the page:

Notice: Undefined variable: googlead in /home/content/r/e/t/retzerj/html/Engine/core/core.functions.php(538) : eval()’d code on line 27

Any thoughts? My reading on PhP tells me that I don’t have to define variables because creating them defines them on the fly ....

Posted by John Retzer on 12/22 at 06:31 PM | Link

John: I suppose you’ve just copied the copy above. Note there is a minor typo in Lyndas first line of code, missing the ? in front of php ... therefore, the variable isn’t initialized and you get the error you mention. It should read <?php… instead of <php…

Posted by Markus on 01/06 at 01:29 PM | Link

Thanks for pointing that out Markus!  I’ve fixed it in the entry above.

Posted by Lynda on 01/06 at 01:32 PM | Link

Hi,

Got this working here - http://www.photographyblog.com - thanks!

Is it possible to adapt it to show more than one ad?

Say, one ad after entry 5 and one after entry 10?

Posted by Mark Goldstein on 02/01 at 12:45 PM | Link
Trackback: HowTo: Add Items Between Entries (Lynda)
Question: How can I add Google Adsense or another type of item between my EE entries? Answer: Lynda of So Very Posh has a great how to article on how to do this: Ads Between Entries Thanks, Lynda for making this available.
Tracked on: EE and pMachine HowTo on 08/30 at 05:41 AM