testing code
By Webmaster,
Client Stage
As someone constantly experimenting with WordPress customization, I wanted to control how and where ads appeared on my archive pages. Instead of relying on ad plugins with bloated features, I took a more efficient route鈥攚riting a custom script to insert ads after every 3rd, 5th, and 7th post dynamically. Here鈥檚 how I did it and why this solution works like a charm.
function kadence_insert_ad_after_posts($post_id) {
global $post_count;
// Initialize counter if not set
if (!isset($post_count)) {
$post_count = 0;
}
$post_count++; // Increment counter with each post
// Insert the ad after the 3rd and 5th posts
if ($post_count == 3 || $post_count == 5 || $post_count == 7) {
echo '<div class="custom-ad">
<h4>Sponsored Ad</h4>
<p>'. do_shortcode('[ADD YOUR SHORTCODE HERE]') .'</p>
</div>';
}
}
add_action('kadence_loop_entry', 'kadence_insert_ad_after_posts', 20);