This question was raised in the StudioPress forums: How to Add Google Adsense After the First (or Second) Paragraph of a Post in Genesis? So here's how I did it initially:
[php]<?php
add_filter( 'the_content', 'my_add_after_first_p', 20 );
function my_add_after_first_p( $content ) {
$ad = '</p><p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxxxx";
/* 468x60, created 10/4/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>';
$content = preg_replace( "/</p>/", $ad, $content, 1 );
return $content;
}
?> [/php]
However, I then came across another method (on @wpbeginner who grab it from somewhere else), which I like better, after the individual wanted it after the 2nd paragraph. So copy the following code and place it in its own php file called adsense.php (or download it here: [download id="9"]).
[php]<?php
$paragraphAfter= 1; //display after the first paragraph, change to 2 for 2nd paragraph
$ad = '</p><p style="text-align: center;">
<script type="text/javascript"><!--google_ad_client = "pub-xxxxxxxxxxxxxxxxxx"; /* 468x60, created 10/4/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></p>';
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++ ) {
if ($i == $paragraphAfter)
echo $ad;
echo $content[$i] . "</p>";
}
?>
[/php]
Then in functions.php add the following:
[php]
add_action('genesis_before_post_content', 'include_adsense');
function include_adsense() {
if ( is_single() )
require_once(CHILD_DIR . '/adsense.php');
}
[/php]
Adam W. Warner says
Great advice, thanks for posting (and reminding me that I was going to experiment with AdSense on one of my sites again). I like the second method, seems more clean even though it’s a two-step process.
Ian says
I’m actually using Thesis right now, but have been considering Genesis.
How easy would it be to include a check box on the edit post/page screen to only allow this on posts of your choosing?
Travis Smith says
With a checkbox, it is as easy (or difficult) as adding a metabox to the code with a single conditional statement. With the custom metabox script, it is quite easy.
Detectiva says
Thank you but it does not work on my site.
When I applied the second option, (after uploading the adsense.php and adding that code into function.php) it simple made my post disappear.
Here is the blog I am talking about, http://detectiva.com/blog/english-to-spanish/good-morning-in-spanish/ and please get back to me asap, thank you a million. 😉
Travis Smith says
It appears that you’ve got it working???
Travis Smith says
Please refer to the plugin version of this code: http://wordpress.org/extend/plugins/gravity-forms-terms-of-service-field/
Lisa says
hi guys, this code is ignoring [/caption] therefore jumping inside captions for my post images. Apparently those words qualify as first paragraph. Any ideas?
Jabran says
Thanks for the wonderful tip. There is however a mistake.
For the second tip:
In line 10 of adsense.php code, the ‘ should be replaced by ‘
(Just a change in the placement of apostrophe)
Jim Rush says
With a few adjustments to the code it works, thank you!
Nguyen Dat Tai says
How to Add Google Adsense After the First in Genesis 2.0?
Thanks!
Michael says
Thanks a lot Travis for your code snippet! I have modified your snippet to work with a regular WordPress install and added in multiple ads. Maybe this helps somebody: http://www.schurpf.com/displaying-ads-after-first-paragraph-with-wordpress-or-after-any-paragraph-you-wish/
Thanks for posting this!
manish kapoor says
I am struggling a lot since last 3 days to add adsense between posts on my website http://www.funnyquotes4u.net. I am using DIVI theme. Can you please help me out?
pakown says
This has solved my problem…thanks
Biplab Acharjee says
Thanks for creating this post Travis Smith. I have got my solution form your post. Its working for me.