Archived topic
How to display ads between posts
7 replies · Started by marv3x on January 30, 2020
Hello, I'm trying to add some adsense between posts in the archives and homepage.
I was trying to use the Ad Inserter plugin, but it doesn't show right the count of posts.
Can it be done by hooks?
Thanks a lot.
Hi there,
Unfortunately there are no hooks between the posts blocks.
A plugin like Ad Inserter is required.
Hope this helps!
Oh :(
Unfortunately, Ad Inserter doesn't work on the theme, in that functionality. Do you know any alternative?
Thanks!
Sorry we are talking about this plugin right?
https://en-ca.wordpress.org/plugins/ad-inserter/
It should work with GP - can you explain the issue a bit more?
And the same issue doesn't exist if you activate a default twenty series theme?
Let me know :)
You can look here:

It does not display the posts correctly. And every category have different orders.
How are you implementing it? Just by activating the plugin?
Does it work out of the box when using a twenty series WP theme?
I apologize if this is not the right place to share this since this is an older topic, but I happened to come across it and I hadn't seen the information elsewhere in the support forums.
I just wanted to share a solution I found for this that worked for me in case anybody else is facing a similar scenario.
I was looking for a way to insert ad code after a certain number of posts (i.e. every nth post) on the archives and home pages without a child theme or plugin.
I created a new hook element set to display on the front page and all archives pages, allowed PHP to execute and attached it to the generate_before_content hook. Then I added this code:
<?php global $loop_counter;
$loop_counter++;
// show ad every 4th post
if ( $loop_counter % 4 == 0 ): ?>
<!-- ad code here -->
<div style="width:728px;height:90px;border:1px solid #000;">
Sample ad space
</div>
<?php endif; ?>
Instead of every 4 posts, you could obviously change it to suit your needs, i.e. every 5 posts, 2 posts, etc.
You could also do something like this to show an ad between specific posts (#2 and #7 in this example):
<?php global $loop_counter;
$loop_counter++;
if ( $loop_counter == 2 or $loop_counter == 7 ): ?>
<!-- ad code here -->
<div style="width:728px;height:90px;border:1px solid #000;">
Sample ad space
</div>
<?php endif; ?>
I hope this is potentially helpful to others!
Thanks for sharing!