How can we insert ads shortcode between looper post grid?

I have been trying to insert adverts between my posts in a looper. I want to insert one advert (using Advanced Ads shortcode) every 2 or 3 posts. For example, a 3-column (on desktop) grid would have an advert on every row.
I can almost do this by using the condition ‘Index is odd’, however, the advert then shares the same space as the post.
Is this possible?

Hello @innovatek,

Thanks for writing in! Yes, that is possible. You can break down your Looper Consumer into 3 parts and insert the ads in between. For example structure:

Section - Looper Provider
   Row - Looper Consumer (display 3 items only)
   Row / Column / Ads display
   Row - Looper Consumer (display another 3 items)
   Row / Column / Ads display
Row - Looper Consumer (display remaining items)

Hope this helps.

Thanks for the quick reply. While this is an improvement to my initial setup, I probably did not explain myself correctly.
What I want to do is have the adverts within the actual grid, so no gaps appear even if there are 1 or 6 ad slots.
For example, if 2 adverts, it would be:

  • Post, Post, Ad
  • Post, Post, Ad
  • Post, Post, Post

For 1 advert, it would be:

  • Post, Post, Ad
  • Post, Post, Post
  • Post, Post, Post

The advert shortcode I use has been set to only allow an individual advert to appear once a page. So if there are 5 adverts, after the 5th time, the shortcode will output nothing.
Another option may be to use an array/PHP to include the shortcode within the looper, but that may be beyond my skills. Advanced Ads has this code, but I haven’t been able to merge this with a ‘simple’ looper array and then linking that with your lopper providers. Is this ‘simple’? Here is their code:

// global $wp_query; // uncomment if loop content is in another file or function
if( function_exists(‘the_ad_placement’) && $wp_query->current_post === 4 ){
the_ad_placement(‘between-posts’);
}

Hello @innovatek,

You can insert the same element as your Looper Consumer. And then inside it, use a Raw Content element where you can insert the Ad shortcode. The structure could be something like:

Section 
   Row - Looper Provider
      Column - Looper Consumer 
         Post Item Item title
         Post Description

      Column - has condition if Looper Item Index is 2 
         Raw Content - [Ad Shortcode 1]

      Column - has condition if Looper Item Index is 4 
         Raw Content - [Ad Shortcode 2]

Hope this helps. Kindly let us know how it goes.

Thanks again. It almost worked with a little trial and error.
Setting it up as you outlined placed the adverts at the end of the list, not between the posts. It also messed with the grid column layout
I did get it to ‘work’ with the outline below, however, I needed to hide the posts with index 2 and 4 (which means these posts never appear).

Section
    Row 1 - looper provider. Layout 3 columns
        Column 1 - Looper Consumer
            Row 1.1 - Layout 1 column
                Column 1.1 -  condition to not display when index is 2 or 4
                    Post elements (title, etc)
                Column 1.2 -  Looper Consumer. Condition to display when index is 2
                    Raw Content - [Ad Shortcode]
                Column 1.3 -  Looper Consumer. Condition to display when index is 4
                    Raw Content - [Ad Shortcode]

This gives me a responsive 3 column grid of posts with adverts in the right place, but I don’t see posts 2 or 4.
See the test page at: https://woodworks.news/test-page/

Also note

  • Row 1 and Column 1 seem like a waste. However, if I simplified by removing them, either the grid completely broken down and/or adverts would only appear after all the posts.
  • If I didn’t have the looper consumer in the advert columns 1.2 and 1.3, the adverts would not appear.

Is there a way to either:

  1. insert a blank ‘space/post’ at index 2 and 4
  2. use a post offset after each advert, but keep things in the same column grid (so it is responsive)
  3. or something else I am missing

Once working, I will be updating to include anywhere from 0 to 10 adverts within the grid.
So we are almost there without needing complex code - yet - just a complex layout that almost works prefectly…
Cheers,
Ken

The other option would be to use a custom looper provider and include a get_post where I can tell it when I want the adverts inserted.
I have the following working to create the base looper post grid and some code Advanced Ads uses to insert the adverts into post loops. I just don’t know how to insert Advanced Ads code.

add_filter( 'cs_looper_custom_post_adverts', function( $result, $args ) {
$args = array(
    'category_name' => 'woodworks',
	'posts_per_page' => '-1'
); 	
	return get_posts( $args );
}, 10, 2);

The Advanced Ads code to insert into the above somehow

// global $wp_query; // uncomment if loop content is in another file or function
if( function_exists('the_ad_placement') && $wp_query->current_post === 4 ){ 
    the_ad_placement('between-posts');
}

Thanks again for your help.

Hello @innovatek,

The Advanced Ads code is irrelevant to the custom Looper code. It will not work. Please provide us with your WP Access so we can check your test page instead. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
– Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

image

Best Regards.

Thanks for your help. I have 3 versions of the grid on the website I was playing around with and I have highlighted these. Please note it is the conditional visibaly one that almost works, but actually hides the posts for the advert - so not right.
I will include the login details in the secured note.

Hello @innovatek,

This is how it can be laid out:

Kindly check out the demo example in the secure note below.

Thanks.

Thanks for your time and help - and sorry for my show response - been a busy week.
The layout looks good. My only issue is that, when there is no advert outputed (via the advert shortcode), that the grid doesn’t hide the advert column and instead show a continuous post output.
Cheers,
Ken

Hi @Innovatek,

Regretfully, that is not possible to recognize that the Advert shortcode does not return anything.

Thanks

Ok, thanks

No problem. Feel free to create another thread if you need any other assistance.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.