Reputation: 305
I've currently got my website set up where it automatically adds a Google Adsense ad after the 2nd paragraph of any article, but I'd like to improve on this, if anyone is able to help.
I'd like to add to this code to add another 2 adverts; one after a 6th paragraph and another after a 10th. If the article doesn't reach these paragraph numbers then the adverts shouldn't show.
It's probably something really obvious but anything I've tried has resulted in the functions.php file crashing when I reload the site.
My code is...
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div class="mobilead .visible-xs-block hidden-sm hidden-md hidden-lg"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXX"
data-ad-slot="1716361890"
data-ad-format="auto"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script></div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $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 );
}
As a supplementary question - is there a way to limit these ads to showing only on posts, rather than pages as well? Currently they're showing anywhere.
Any help would be hugely appreciated.
Upvotes: 4
Views: 1792
Reputation: 3496
There is too much wrong with your ad code for me to attempt guessing what it should be (it has an opening <div>
but no closing </div>
, it has what appears to be javascript outside of a <script>
tag)
...so I skip that part, and simply show how to insert another p
aragraph instead - this will insert something in the spots you want, and also shows how to use get_post_type()
to ensure ads are only shown on posts:
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
//The last condition here ensures that ads are only added to posts
if ( is_single() && !is_admin() && get_post_type() === 'post' ) {
return prefix_insert_ads( $content );
}
return $content;
}
function prefix_insert_ads( $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
$paragraphs[$index] .= $closing_p;
if ( in_array($index, array(1, 5, 9)) ) {
//Replace the html here with a valid version of your ad code
$paragraphs[$index] .= '<p style="background:#f00">Ad goes here</p>';
}
}
return implode( '', $paragraphs );
}
Upvotes: 4
Reputation: 3683
Check functions in the section of Conditional Tags Index from https://codex.wordpress.org/Function_Reference
if(!is_page()) {
// do your tricks
}
There're also some other functions you may need like is_home()
, is_front_page()
and etc.
Upvotes: 0