Possibility to add multiple inline ads after multiple paragraphs within content

Posted in: Newspaper
Post count: 436

Hi,
Few months before Catalin explained to me how to add custom ads at different position under different posts here- https://forum.tagdiv.com/topic/change-the-default-position-of-bottom-ad/

Is there any way to use the same method to insert 2 or 3 custom ads within post content after multiple paragraphs. There is already a given option to add one inline ad after the desired paragraph but since these days most of the visits comes from inline ads on mobile sites, I want to include 3 inline ads.

How can we do that? Please help me with this.

Thank you.

Post count: 22421

Hi

Sorry but the theme was designed to add only one article inline ad in post content using the spot from theme panel. To add multiple ads after a specific number of paragraphs. It will have to be made custom but we have not tested adding multiple inline ads. Sorry.
The code is found here if you want to have a go at it: https://www.screencast.com/t/Nr8U6aBIVt

Thanks!

Post count: 436

Hi Bogdan,
After searching a lot on the internet and trying out many things. Here is the code I am sharing for others to use which worked for me.

To add Adsense or any kind of ad codes at after multiple paragraphs. Simply copy this code at the end of functions.php



//Insert ads after second and fifth paragraph of single post content and after the post content.  Provided by Bloggingdude.com:

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	
	$ad_code = '<div> Adsense/Ad code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}
	
	return $content;
}

add_filter( 'the_content', 'prefix_insert_post_adsNew' );

function prefix_insert_post_adsNew( $content ) {
	
	$ad_codenew = '<div> Adsense/Ad code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_codenew, 5, $content );
	}
	
	return $content;
}
 
// Parent Function that makes the magic happen
 
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 );
}
// Add signature or ad after post content. Provided by Bloggingdude.com:

function custom_content_after_post($content){
if (is_single()) {	
	$content .= '<div> Adsense/Ad code goes here</div>';
}
	return $content;
}
add_filter( "the_content", "custom_content_after_post" );

Simply change the text ‘Adsense/Ad code goes here’ with your own Adsense/ad codes and it will start displaying ads after paragraph-2, 5 and after the post content.

You can change the paragraph number 2 and 5 to your own desired paragraphs.

The only problem I am having with any of the methods I use to insert inline ads is that it is compatible with TagDiv Speed booster plugin. No, I am not talking about only the method I wrote above, but no matter how you include inline responsive ads after multiple paragraphs, it breaks when activating speed booster plugin.

I have raised this issue in detail here: https://forum.tagdiv.com/topic/conflict-between-speed-booster-and-inline-ads-what-can-be-done/ . See if there is any possibility to resolve it.

Thank you 🙂

  • This reply was modified 9 years by Nano.
  • This reply was modified 9 years by Nano.
  • This reply was modified 9 years by Nano.
Post count: 22421

Thank you for sharing this solution. I;lm sure it will help other users as well.
We will look into the ads problem from the other topic as well.
Thanks!

Post count: 135

Just want to say thanks for this!

Post count: 436

Your welcome @crazydiver 🙂

Post count: 58

yep, I’ve been trying to figure this out for ages. Works like a charm. Thanks for the code.

Post count: 13

Does it works for the tagDiv Mobile Theme too?

Post count: 20688

Hi,


@Diniska
It may work, but it would have to be entered in the mobile theme functions file
https://www.screencast.com/t/snf5sdLnJN
This means that it will be lost after a theme and plugin update, and has to be entered again.

Viewing 9 posts - 1 through 9 (of 9 total)
The forum ‘Newspaper’ is closed to new topics and replies.