Inserts Ads After a Few Paragraphs

Putting ads on your WordPress site is quite a personal choice: for some it will be one of the first things you do, while others will resist it for a while. After quite a while of refusing to put ads on my site, I have finally decided to put up some ads that advertise my own services, not least to educate my readers that I do offer things like WordPress hosting packages and WordPress consulting services.

I created a few ads in the standard 125×125 and 468×60 sizes. The intention was to put the 468×60 ads at the bottom of the article, and the 125×125 ads at the beginning of the article. However, after trying that, I didn’t like how an ad was the first thing your eyes see when trying to read an article – the emphasis of my site is on informative WordPress tutorials, not the ads. So I wanted to set about moving it down the page slightly.

This seemed like a simple idea, but there was more to it. The standard call for displaying your WordPress content (the_content) is just a single line that automatically pulls the entire post from the database from beginning to end, so inserting something in the middle of it is not as easy as it sounds.

However, you can use the following code in place of your content call to enable you to place your ad code (or any other type of code) after a set number of paragraphs. So, open up your theme editor and open single.php for editing and look for the following line:

Now you’ll want to replace that with the following code snippet. Don’t worry about it’s apparent complexity – there’s only two simple variables in there that you will need to change:

In this example, the ad will be displayed after the second paragraph. This is as a result of the $show_after_p = 2 line near the top of the snippet. You can change this to another value to suit your needs.

The other thing you will need to change is the ad code itself. Two thirds of the way down, you will see a spot that says YOUR AD CODE GOES HERE. This is where you should place your AdSense or other code.

Since I wanted to use ads that were advertising my own sites, I went with an internal ad control plugin. After reading an article on DataFeedr Random Ads v2, it sounded like a perfect fit for my needs. You can use it with ad publishers’ codes (like AdSense or BuySellAds) and also sprinkle in your own ads.

Yes, it will insert the ads above whatever paragraph you specify. I don’t know off hand how to show an ad above the first picture in your post, unless you’re talking about the featured image, in which case you can just modify your theme where it calls your featured image.

Hi,Dave,
I know a lot of people who use Easy Adsense plugins, but the fact that you can control the placement of your adds seems to be more better choice. This sounds very simple to do, especially when you made the effort to present it in such an easy way, so it`s just left for us to copy-paste your examples.
Thanks a lot for sharing this.

The “Quick Adsense” plugin supports this (and lots of options – after any paragraph you want, rotating ads…). It is very helpful to see the code to do these things manually (because sometimes that is best). But using plugins that are well done is so easy :-)

One thing people have to remember any time they start editing the templates directly is to remember that they may need to do those again after updating (if the theme updates that file, your changes will be lost…).

Agreed. The best way to do it would be to create a function and put it in your functionality plugin to avoid changes being overwritten by theme upgrades. I’ve tried to write my more recent tutorials in this fashion, to avoid editing theme files at all costs.

Thats why you should always build a child theme when you modify your wordpress theme. This avoids to loose your changes when you update the main theme core files. It takes just seconds to build the child theme but will save you lots of trouble ;)

wp_link_pages should be inserted in any well-coded theme. Some plugins or snippets will break the pagination of (this perhaps an example), and it is usually because wp_link_pages is not present in single.php, so make sure to put it into the theme and that will hopefully resolve it.

Thanks for the reply.
As stated, w/o the above code, pagination works; wp_link_pages is in the theme, outside of the above code. Pardon if I was unclear…I need to now where and how to place wp_link_pages inside the above code to make that function work again.

Ahh, then I did misunderstand. If the theme already includes wp_link_pages, then you don’t need to include it in the code above; it should work by itself. I’m surprised that the above code broke it. I’m not sure what the issue is I’m afraid.

Dave, isn’t der an else statement missing for the main if statement? I had no output on posts where paragraph count is less then show_after_p. And whats the reason for this main if statement at all? Don’t the p_count == $show_after_p if statement be enough to render the content without Ad when there are less paragraphs present? I am thinking about dropping the mein if statement.

BTW. WordPress uses p tags in image captions as well. If you have an image with a caption in your content before the show_after_p paragraph, this is a problem. I took your code and modified it slightly (second code example):http://www.beeze.de/2013/adsense-nach-dem-ersten-absatz-1243
I used the clean opening p tags to avoid the caption p tags, which come with additional css class.

In the third and fourth code example I add additional Adsense blocks at the post end and post middle depending on the overall post length.

This code is amazing!! However, I’m not too versed in PHP coding, so maybe you can help. My theme calls out the “Read More” link in a unique way so my php the_content code looks a little different.

How do I get the code that is sitting in the usually blank area between the parentheses to translate into your code??! If I simply remove it, my site and individual articles work fine, but I no longer see the excerpt for the posts listed on my homepage…it just shows the whole article.

If I use this line: $content = apply_filters(‘the_content’, get_the_content( __( ‘Read More…’, ‘cudazi’ ) ), $post->post_content);
then I no long see parts of my content on the homepage…just the title and featured image. Any ideas?

The read more link only shows when you’re not viewing the single post (i.e. on the home page or in the archives). So, this doesn’t apply to your situation, because it sounds like you’re trying to insert something in between a few posts on your front page, rather than in between paragraphs in an article

Great code, thanks. However you need to add an “else” clause for instances where the post contains less paragraphs than defined in “show_after_p”. In the given example I admit it’s unlikely but people may use this code to add adverts much further down the post.

} else {
the_content();
}

would suffice – that way the post would still display even if it contained less paragraphs than defined in “show_after_p”.

Wish your site was as fast as this?

Do It With WordPress is proudly hosted by WP Engine, the very best WordPress hosting that money can buy.

Aside from being blazing fast, it's very secure, has a staging area to test changes before making them live, automated daily backups, malware scanning, stellar support from WordPress experts... It's just everything that you would want from your host, and more.