How to Display Adsense at the Middle Of WordPress Post

Recently, i was experimenting with Adsense placement to determine which positions will best generate revenue for me, i decided to try adding an Adsense unit at the middle of my post content. I wanted to do this without plugin ‘cos am not a fan of plugins rather i prefer using snippet of code to achieve my set goals.

Though am a newbie when it comes to PHP programming, i tried out some few tricks i know, but still without success until i stumble on this one am going to share.
Believe me, it work like charm on my first attempt.
Follow the few steps below:-

  • Login to your WordPress dashboard and navigate to Appearance -> Editor, if your theme didn’t show up, select it from the dropdown menu at the top-right corner. Or use a ftp client to navigate to ./public_html/wp-content/themes/your theme folder
  • Open the single.php file, find and replace < ?php the_content(); ?> with
    
    < ?php
    $content = apply_filters('the_content', $post->post_content);
    $content = explode (' ', $content);
    $halfway_mark = ceil(count($content) / 2);
    $first_half_content = implode(' ', array_slice($content, 0, $halfway_mark));
    $second_half_content = implode(' ', array_slice($content, $halfway_mark));
    echo $first_half_content.'...';         echo ' YOUR ADSENSE CODE OR AFFILIATE LINK HERE';
          echo $second_half_content;
    ?>
    
  • Replace YOUR ADSENSE CODE OR AFFILIATE LINK HERE with your own code.
  • Save and then preview

If you’re having difficulties implementing this in your theme, or you have an alternate means of doing this, kindly use the comment below.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address