Show Google AdSense Ads After Post without Plugins

Show google AdSense ads on blog feed after 2 , 3, 4, 5, 6 or what’s your choice. So modify your index.php file code with given example.

Example code

<?php
/**
* Main template file.
*
* @package thekroyaards Example
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="post-list">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
$postCount = 0;
while ($loop->have_posts()) : $loop->the_post();
$postCount++;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_content(); ?>
</div>
</article>
<?php
// Display AdSense ad after every 4 posts
if ($postCount % 4 === 0) {
echo '<div class="adsense-ad"><!-- Place your AdSense ad code here --></div>';
}
endwhile;
?>
</div><!-- .post-list -->
</main><!-- #main -->
</div><!-- #primary -->
<?php get_footer(); ?>

This is an example of implementation on the code of your Index.php

How Can Do

  1. Open your theme’s index.php file.
  2. Paste these example’s code on your index.php file.
  3. Now change post count % 4 to your desire post number. If you set post count % 5, So Ads show after 5 post feed.

Use this code on your custom theme and you don’t need to use any type of plugins for showing feed ads.