0


Have you seen blogs that has ads inside their post content? These ads are either after the first paragraph or the second paragraph in most cases. Most beginners wonder whether these sites insert ads manually when they write the content, or if there is a special code for this. It would seem extremely inefficient if one enters the code manually specially if you had to switch advertisers after one year and you have over 200 articles. This article will show you a snippet that will let you enter ads within the post content after the first paragraph.
First open your single.php file and look for the content code that would look something like this:
How to Insert Ads within your Post Content in WordPress

Have you seen blogs that has ads inside their post content? These ads are either after the first paragraph or the second paragraph in most cases. Most beginners wonder whether these sites insert ads manually when they write the content, or if there is a special code for this. It would seem extremely inefficient if one enters the code manually specially if you had to switch advertisers after one year and you have over 200 articles. This article will show you a snippet that will let you enter ads within the post content after the first paragraph.
First open your single.php file and look for the content code that would look something like this:
1 | <?php the_content(); ?> |
Replace that code with the codes below:
01 | <?php |
02 | $paragraphAfter = 3; //display after the first paragraph |
03 | $content = apply_filters( 'the_content' , get_the_content()); |
04 | $content = explode ( "</p>" , $content ); |
05 | for ( $i = 0; $i < count ( $content ); $i ++ ) { |
06 | if ( $i == $paragraphAfter ) { ?> |
07 | <div>Insert Ads Here</div> |
08 | <?php } |
09 | echo $content [ $i ] . "</p>" ; |
10 | } ?> |
You may change the number of paragraphs by changing $paragraphAfter line.
Source of this article is suspended by the host, we used Google Cache to get this tutorial and it works.

Subscribe to:
Post Comments (Atom)
0 Responses to “How to Insert Ads within your Post Content in WordPress”
Post a Comment