Skip Navigation

[Resolved] How to: Insert an advert every x results in view loop

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a view that loops through a CPT. I want to insert an advert (from another CPT) every x number of results listed in the loop section.

Solution:

You can try with the Views shortcode [wpv-item], for example:

https://toolset.com/forums/topic/how-to-insert-an-advert-every-x-results-in-view-loop/#post-1147801

Relevant Documentation:

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-item

This support ticket is created 6 years 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.

Our next available supporter will start replying to tickets in about 3.88 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by bradleyS 5 years, 12 months ago.

Assisted by: Luo Yang.

Author
Posts
#1147770

Tell us what you are trying to do?

I have a view that loops through a CPT. I want to insert an advert (from another CPT) every x number of results listed in the loop section. Could you point me in the right direction please?

What is the link to your site?
hidden link

#1147801

Hello,

You can try with the Views shortcode [wpv-item], for example, edit your view, in section "Loop Editor", display an AD codes after every 3 items, like this:

...
	<!-- wpv-loop-start -->
	<ul>
	<wpv-loop wrap="4" pad="true">
		[wpv-item index=1]
				<li>[wpv-post-link]</li>
		[wpv-item index=other]
				<li>[wpv-post-link]</li>
		[wpv-item index=4]
				Here put your AD codes
	</wpv-loop>
    </ul>
	<!-- wpv-loop-end -->
...

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-item
Sets a condition to be met for subsequent lines in the loop to be executed.

#1147802

Thank you Luo.

Would this work?

[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="Loop item in Air Adventures"]
      	[wpv-item index=split2]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
#1147810

It should be able to work too.

The attribute "split2" works like this, if there are 12 results in this page, it will split the results in two part, and display your AD codes after the 6th item, for example:

[wpv-items-found]
    <!-- wpv-loop-start -->
    <wpv-loop>
        [wpv-post-body view_template="Loop item in Air Adventures"]
        [wpv-item index=split2]
<!-- Here put your AD codes -->
    </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]

See the document I mentioned above:
index=split2 will target the item that falls in the middle of the number of displayed items. In a View displaying 10 or 11 items (set by a limit or pagination), this will target the 5th issue (because it is a “floored middle”).

#1148001
screenshot 2018-11-16 at 16.12.08.png

Thank you Luo.

I've got it working (sort of).

See example here: hidden link

Here's the loop code:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-land-adventures"]
      	[wpv-item index=split5]
      	[wpv-view name="banner-ad-leaderboard"]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No Land Adventures matching your search criteria were found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

I'm displaying 30 results per page and have 'Infinite Scrolling' enabled.

The Banner Ad View
The Banner Ad view that I'm displaying on the split5 has 'Pagination enabled with automatic AJAX transition' enabled, with a 'Show each page for:' 12 seconds. See attached image.

If you look at the See example here: hidden link page, you'll see that the first instance of the Banner add functions correctly and fades between ads every 12 seconds. The problem is that the subsequent inserts of the view don't loop through the ads.

Any idea how to correct this behaviour?

Thx

#1148949

I have checked the example URL you mentioned above, there isn't "instance of the Banner" as you mentioned above, if you need more assistance for it, please provide a test site with same problem, I need to test and debug it in a live website, thanks

#1149078

Thanks for the details:
I can see the banner you mentioned above, and for the problem:
the subsequent inserts of the view don't loop through the ads.

I assume you are going to display the ADs as below:
First banner place, display "Banner Ads" post A
Second banner place, display another different "Banner Ads" post B
...

There isn't such kind of built-in feature within Views plugin, please check the post view "Banner Ad - Leaderboard":
hidden link

You are using random order, each time when display the view "Banner Ad - Leaderboard", Views will query the database again, output a random "Banner Ads" post, so it might conduct the problem you mentioned above: duplicated items

#1149130

Hi Luo,


I assume you are going to display the ADs as below:
First banner place, display "Banner Ads" post A
Second banner place, display another different "Banner Ads" post B
...

No, your assumption above is incorrect.

The "Banner Ad - Leaderboard" view is set to fade between the 'posts' (images), similar to an image slider. The same "Banner Ad - Leaderboard" view is then set to be displayed every x many Adventure posts by the

[wpv-item index=split5]

shortcode.

The first rendering in the fron-end, of the "Banner Ad - Leaderboard" view, fades through each image (page) in the "Banner Ads" CPT.

The following "Banner Ad - Leaderboard" views don't fade between the images (posts) in the "Banner Ads" CPT. This is the issue I'm asking about.

The "Banner Ad - Leaderboard" view ordering is 'Last Modified' - not random.

The "Banner Ad - Leaderboard" - 'Pagination' is AJAX + 'Pre-load 10 pages. I don't expect to ever have more than 10 "Banner Ads" CPT posts (images). So was looking to there only every being one query in the "Banner Ad - Leaderboard" view loop.

Hope this clarifies the query.

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/ajax-pagination-does-not-work-in-same-page-with-multiple-views/

#1149681

Thanks for the details, it is a different problem from your original question
https://toolset.com/forums/topic/how-to-insert-an-advert-every-x-results-in-view-loop/#post-1147770
I have split a new ticket for it, please follow up it here:
https://toolset.com/forums/topic/ajax-pagination-does-not-work-in-same-page-with-multiple-views/

#1149780

My issue is resolved now. Thank you!