Insert single ad from PHP generated list
-
A client sells ad space directly to local businesses. Each advertiser provides a jpg to post on her website. To make this work, I set up a custom post type combined with Advanced Custom Fields where she uploads the ad image, the ad link, and the begin and end dates. I use PHP to extract the active ads for positioning in several locations and have defined eight different blocks in Ad Inserter, which vary on the destination page and the device used. For 6 AI blocks, I use widgets, and these are working fine.
The remaining 2 blocks appear on the home page and on archives/category/search pages on mobile devices. For these pages, I want to automatically insert ads after every other article content, i.e., after 1, 3, 5, etc. And the order of the ads should be randomized with every page load.
I have the PHP code to create a list of the desired number of active ads in random order. Here’s the general format of the list:
<div class="general-ad ad-counter-1"><a href="https://advertiser1.com" target="_blank" ><img src="ad1.jpg"></a></div> |rotate| <div class="general-ad ad-counter-2"><a href="https://advertiser2.com" target="_blank" ><img src="ad2.jpg"></a></div> |rotate| <div class="general-ad ad-counter-3"><a href="https://advertiser3.com" target="_blank" ><img src="ad3.jpg"></a></div>
The settings:
Automatic Insertion: After Content
Filter insertions: 1,3,5,7 using Auto counter
Use client-side detection to show only on Tablet and PhoneThe ads appear in the locations where I expect them, but sometimes one ad appears twice and another doesn’t appear at all.
When I originally tested with lots of ads, I thought that putting |rotate| between the the ads worked. Now I see that |rotate| simply chooses 1 ad at random from my list to place after post 1, then randomly chooses 1 ad from the list (with replacement) for placing after post 3. When my client has only 2 active ads, there’s a 50% chance that the ad after post 1 will be the same as the ad after post 3.
Without the |rotate| between ads, the ads in the list are treated as a block and all the ads in the list appear after post 1, after post 3, etc.
Two solutions that I see are either a) have Ad Inserter randomly choose an ad without replacement from my ad list or b) instruct Ad Inserter to place ad #1 from my list after post 1, then place ad #2 after post 3, ad #3 after post 5, etc.
What is the syntax to achieve either a) or b) or is there another approach that I should use?
Thanks,
Tim
- The topic ‘Insert single ad from PHP generated list’ is closed to new replies.