Skip Navigation

[Resolved] Adding Ad code for every "n" in a loop

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 15 replies, has 2 voices.

Last updated by Noman 6 years, 10 months ago.

Assisted by: Noman.

Author
Posts
#535693

I am trying to: add an ad for every x number of items returned in a view loop

I visited this URL: hidden link

I expected to see: nothing - i dont know how to do this 🙂

Instead, I got: the normal output. note - this is a staging site, so ads wont show, so i can echo some content to test instead for testing purposes

#535771

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Register_Custom_Shortcode.png

Hi John,

Thank you for getting in touch with us. We can do this using Conditional output in the View and using an incrementor.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

1. Please add this code in your theme’s or child theme’s functions.php file

add_shortcode('incrementor', 'incrementor');
function incrementor() {
	static $i = 1;
	$n = $i ++;
	if( $n % 3 == 0 ) // 3 can be change. 
		return 1;
	else
		return 0;
}

==> Whereas 3 is the number of posts / items and you can change it, depending on after how many posts you would like to display Ads.

2. Register the ‘incrementor’ shortcode first in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments (see attached screenshot: Register custom shortcode.png)

3. Then please use this shortcode in your View’s Conditional output something like this:

[wpv-post-link] 
 
[wpv-conditional if="( '[incrementor]' eq '1' )"] <br>
Ad Goes Here...
[/wpv-conditional]

I hope this helps, Thank you

#535818
Screen Shot 2017-06-12 at 4.44.28 PM.png
Screen Shot 2017-06-12 at 4.47.49 PM.png
Screen Shot 2017-06-12 at 3.46.05 PM.png

This looks promising, cool stuff once it gets working. The ad code is not showing in the page source after flushing all caches though. attached are pics of what I've done this far:

Example pages hidden link
hidden link

#536012

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello John,

1. Are you able to see normal text inside this same condition?

2. Is the ad code working in a normal page?

3. Can you please send us ad code snippet, so I can try to add that in my test site and see what happens. I assume the code snippet works like google adsense or similar services.

Thank you

#536912
Screen Shot 2017-06-14 at 3.19.13 PM.png

1. No, it still won't even print dummy text yet, and the dummy text is not visible in the source code (staging site - hidden link.

2. Yes, the ad codes work in general on the live site hidden link. When looking at the source code when i tried to incorporate the ads, I did not see the ad code printing.

3. ad codes are implemented on the live site using this type of example: <div id="ezoic-pub-ad-placeholder-622"></div>. The Ezoic Ad server then decides if it wants to use that ad code or not during its testing process. Each page on the live site contains several ad codes you can see in the page source, but only a few will actually pull the full ad code from the Ezoic server as part of its ad testing process (not sure if that makes sense).

If we can get the function to print dummy code, I think the ads will work.

#537008

Hi Noman,

Actually, your code is working now (maybe I didn't clear one of the caches? Anyways, thanks!!

I have another request, and I don't want to wear out my welcome here, but... Instead of the two ad codes I have listed above, how might I add a sequenced array of ad codes into this loop? In other words, if i have an array of 10 ad codes, how might i be able to drop those into the loop one at a time as the loop proceeds?

Let me know if this is more of a custom programming request - I can ask if my dev if he might know how to program this.

Many thanks!

#537073

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello John,

Yes, I need to do more custom coding for this and I want to see your array list so that we will implement this to your site.

Also, do you want to display ad from array list continuously or it should display after n items (for example 3 or 5 items)?

I have also enabled private message box, please send us temporary access (WP-Admin and FTP Login info) to your staging site.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Links to the [View] Edit Screen

Looking forward to help you, Thank you

#539368

I'd like to drop in one line of the following ad code in sequence every 9 runs through the loop, as each ad code may be unique (I have the incrementor set at 9). Let me know if that makes sense.

<div id="ezoic-pub-ad-placeholder-629"></div>
<div id="ezoic-pub-ad-placeholder-630"></div>
<div id="ezoic-pub-ad-placeholder-628"></div>
<div id="ezoic-pub-ad-placeholder-632"></div>
<div id="ezoic-pub-ad-placeholder-633"></div>
<div id="ezoic-pub-ad-placeholder-634"></div>
<div id="ezoic-pub-ad-placeholder-635"></div>
<div id="ezoic-pub-ad-placeholder-636"></div>
<div id="ezoic-pub-ad-placeholder-637"></div>
<div id="ezoic-pub-ad-placeholder-638"></div>
<div id="ezoic-pub-ad-placeholder-639"></div>
<div id="ezoic-pub-ad-placeholder-640"></div>
<div id="ezoic-pub-ad-placeholder-641"></div>
<div id="ezoic-pub-ad-placeholder-642"></div>
<div id="ezoic-pub-ad-placeholder-643"></div>

#539584

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

verification code.jpg

Hi John,

Okay so we want to add unique ad code after each 9 items, the initial code I gave was if we are using one same ad script but I can modify code to achieve this. I tried to login to WP but its asking for verification code (screenshot attached) but I can't see it anywhere, can you please send me verification code so I can login?

Thank you

#539691

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello John,

1. I have created this at my end. Please add below code in your theme’s or child theme’s functions.php file:

add_shortcode('ad_incrementor', 'ad_incrementor');
function ad_incrementor($atts, $content = null) {
	
	extract( shortcode_atts( array(
        'n_number' => '3',
    ), $atts ) );
	
	$ad_array = explode(',' , $content);
	
	static $i = 1;
	static $j = 0;
	$n = $i ++;
	if( $n % $n_number == 0 ) {
		$m = $j; 
		$j++;
		
		return $ad_array[$m];
	}
}

2. Then use shortcode in the View like this:

[ad_incrementor n_number="2" ]
<div id="ezoic-pub-ad-placeholder-629">629</div>,
<div id="ezoic-pub-ad-placeholder-630">630</div>,
<div id="ezoic-pub-ad-placeholder-628">628</div>,
<div id="ezoic-pub-ad-placeholder-632">632</div>,
<div id="ezoic-pub-ad-placeholder-633">633</div>,
<div id="ezoic-pub-ad-placeholder-634">634</div>,
<div id="ezoic-pub-ad-placeholder-635">635</div>,
<div id="ezoic-pub-ad-placeholder-636">636</div>,
<div id="ezoic-pub-ad-placeholder-637">637</div>,
<div id="ezoic-pub-ad-placeholder-638">638</div>,
<div id="ezoic-pub-ad-placeholder-639">639</div>,
<div id="ezoic-pub-ad-placeholder-640">640</div>,
<div id="ezoic-pub-ad-placeholder-641">641</div>,
<div id="ezoic-pub-ad-placeholder-642">642</div>,
<div id="ezoic-pub-ad-placeholder-643">643</div>
[/ad_incrementor]

==> Please note that , (comma) is necessary.
==> n_number = for putting the value / after n number of items.

I hope this will resolve the problem completely now, please let me know. Thank you

#539720

if you still have the widow or session active the, the verification code is 63020091. But i should probably get your email as that is how the verification code is sent

#539738

Ok, I implemented in the live site and the ad code is printing - will see when the ad system starts to drop the codes in - looks great so far, thx for this custom solution!

#539741

Question noman, if i wanted the incrementor to begin counting at the 5th cycle in the loop instead of counting from the first, do I change static $i = 1; to static $i = 5; ?

#539789

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

No, it will need more changes to do that. When you need that logic, please open a new ticket and assign that to me directly, I will take a look at it then.

Thank you

#540971

Hi Noman,

The ads weren't showing with the above code, so I amended it with the below:

[ad_incrementor n_number="10" ]
<li><div id="ezoic-pub-ad-placeholder-629"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-630"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-644"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-628"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-632"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-633"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-634"></div></li>,
          <!-- Ezoic - native loop #2 mid content - native_side_top -->
<li><div id="ezoic-pub-ad-placeholder-645"></div></li>,
<!-- End Ezoic - native loop #2 mid content - native_side_top -->
<li><div id="ezoic-pub-ad-placeholder-635"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-636"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-637"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-638"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-639"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-640"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-641"></div></li>,
<li><div id="ezoic-pub-ad-placeholder-642"></div></li>,
          <li><div id="ezoic-pub-ad-placeholder-643"></div></li>,
[/ad_incrementor]

Then the ads began displaying (and it added a blank

  • when the ads were not called - that's fine.

    BUT - soon after the site crashed, my VPS CPU went to 100%! The site was inoperable for about 6 hours until I figured out that the Caching Plugin (W3TC) was going berserk (my sysadmin explained why, I forgot the technical reason). Once I disabled the plugin, it settled down and all is cool - whew.

    Unless you have any guidance with the above code, we can consider this done - thank much for the detailed coding help - above and beyond man!

    john

  • This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.