This post belong to those who want to insert ads between post content in WordPress website. You have seen most of websites who showing advertisement between post content. Some beginner think they added ads manually in each post or they have fixed layout for it. Lets explain you how you can insert ads between posts after specific paragraph.
There are many ways to show ads between content or in post page. Many ready plugins available for it, but if you don’t want to use plugin than we can achieve this via some code changes.
We are going to explore both ways to insert ads between posts. First start with some popular plugins, which add functionality to place ads anywhere on WordPress.
Insert Ads Between Posts with Use of Plugins:
Here is the some plugins details, which help you to manage advertisement placement on website. These WordPress plugins are free to use and easy to manage.
1) Ad Inserter
Ad Inserter plugin comes with some advanced advertising features like Adsense code integration, like analytics and tracking data. Also support all kinds of Advertisement including Media.net, Google AdSense, Chitka
etc.
This plugin automatically allow to insert ads between posts and pages. Its provide 16 Ads code blocks with code preview feature.
The free version support 16 Ads code blocks. If you want more ads block than go for Pro paid version. Easily manage to display ads on posts or pages.
2) Insert Post Ads
Insert Post Ads plugin is easy to use and allow you to automatically insert post ads after a specified number of paragraphs. Just install this plugin and activate it. You see Post Adverts menu option at left side.
Click on Post Adverts -> Add New option, insert ad code on it and choose paragraph, after you want to show that ad.
Under settings option you can manage the advertisement placement. Choose any option where you want to show ads like Page, Posts and custom posts.
3) AdSense Plugin WP QUADS
Wp Quads is another free plugin with over 90,000 + installations. This plugin gives good user experience and control on showing ads on post or page. Easy to manage ads visibility, position and size.
After activating this plugin, new menu item WP QUADS added. Click on it and place your advertisement code in AD CODE section. There are many ad block showing, you can place ad in any block. Also choose the layout, margin to show ads.
After placing ads in different ‘AD Code’ block you can manage the showing position under “GENERAL & POSITION” section. Their is option to show how many ads you want to show on post or page.
Choose any ad code block in drop down position list. Its gives many option like Beginning of post, middle of post, end of post or right after <-more-> tag.
Using above mentioned plugin are easy to use and completly free. This is good option for beginners.
But using these plugin might be slow down your website loading speed. We have another way to insert ads between post. This is quite technical, but you can manage it easily. Lets check it out…
Insert Ads in Post Content Without Plugins:
This method require some code editing process. Follow below steps:
1) Open your theme function.php file in text editor.
2) Insert the following script code at the end of this file, before the ‘?>’ tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//Insert ads after third paragraph of post content. add_filter( 'the_content', 'insert_post_ads' ); function insert_post_ads( $content ) { $ad_code = '<div>Insert your Ads code here</div>'; if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 3, $content ); } return $content; } // Parent Function function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); } |
3) Now update the function.php file, thats it.
You can change the Ads position on post by changing this code line. For example need to place ads after fifth paragraph than change the value by 5 number.
return prefix_insert_after_paragraph( $ad_code, 5, $content );
Show Multiple Ads in between Post Content:
If you want to place multiple Ads on single post page like first Ad on after 3rd paragraph and second Ad after 6th paragraph than do this change in code.
1 2 3 4 5 6 7 8 9 10 11 |
$first_ad_code = ‘Ads code goes here’; $second_ad_code = $first_ad_code; if ( is_single() && ! is_admin() ) { $content = prefix_insert_after_paragraph( $first_ad_code, 3, $content ); $content = prefix_insert_after_paragraph( $second_ad_code, 6, $content ); return $content; } return $content; } |
Show Ads via WordPress Widget
This is another way to show Ads on post or page. WordPress themes has some ready widgets, where you can place any HTML/PHP/SCRIPT code.
For example you want to show Ads on Post sidebar. Than go for Appearance -> Widget section.
There is Custom HTML widget, just drag and drop it in sidebar section. Insert Ads code in this widget section and save it. Thats it. Ad will start showing on every Post or Page, where sidebar exist.
Conclusion:
You can use any way to insert Ads between posts in WordPress website. If you going for second option without plugin, make sure take backup of your website. Hope this article help you to achieve this.
Thanks for the code.
I have implemented first code and it perfectly work. But iam not good at coding so can you tell me how to show multiple ads? This second code how to implement it properly can you help me with it?
Also for multiple ads do i need to create second in article ads code? Or i can use the same ads code twice?
Hi Ashikur,
For multiple ads, you need to create seperate ad code for each one. Don’t use same code twice.
And to show multiple ads with in post content, i have mentioned multiple ads code in article. You can use like this:
$first_ad_code = ‘First ad code goes here’;
$second_ad_code = ‘Second ad code goes here’;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $first_ad_code, 2, $content );
$content = prefix_insert_after_paragraph( $second_ad_code, 4, $content );
return $content;
}
return $content;
}
tnx for replying.
sry to bother you.
i have successfully implemented first code(which is single ad showing code)
i have second code from your post.
but what iam not understanding is to where i paste the second code(after putting both adsesnse code)
Can You update this post with this:
like first Ad on after 3rd paragraph and second Ad after 6th paragraph than do this change in code.
making a combination of code as one.
i know iam making confuse you.
i just want one code instead of two, where i just paste two adsense code and be done with it
will
$first_ad_code = ‘Ads code goes here’;
$second_ad_code = $first_ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $first_ad_code, 3, $content );
$content = prefix_insert_after_paragraph( $second_ad_code, 6, $content );
return $content;
}
return $content;
}
this code work? without adding any other code from first one?
You have to add this function prefix_insert_after_paragraph() code too.
$first_ad_code = ‘Ads code goes here’;
$second_ad_code = $first_ad_code;
if ( is_single() && ! is_admin() ) {
$content = prefix_insert_after_paragraph( $first_ad_code, 3, $content );
$content = prefix_insert_after_paragraph( $second_ad_code, 6, $content );
return $content;
}
return $content;
}
// Parent Function
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content )
{
$closing_p = ”;
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( ”, $paragraphs );
}
is it okay now?
if not pls can you add it properly and show me?
Thank you very much, your code really helped me! I’ve changed it a bit, so I could set positions for headings instead of paragraphs.