Answer the question
In order to leave comments, you need to log in
How to remove extra P tag after inserting AdSense?
Good afternoon.
Today I discovered this feature. In general, on a wordpress site, I use the following code to insert AdSense after the 1st paragraph:
add_filter( 'the_content', 'wpse_ad_content' );
function wpse_ad_content( $content ) {
if( !is_single() )
return $content;
$paragraphAfter = 1; //Введите номер абзаца, после которого вставляем.
$content = explode ( "</p>", $content );
$new_content = '';
for ( $i = 0; $i < count ( $content ); $i ++ ) {
if ( $i == $paragraphAfter ) {
$new_content .= '<div class="alignleft" style="height: 250px;width: 300px;margin: 0px 10px 0px 0px;padding: 10px;background: #D0DCE7;">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-8519272212979780"
data-ad-slot="7882244559"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>';
}
$new_content .= $content[$i] . "</p>";
}
return $new_content;
}
Answer the question
In order to leave comments, you need to log in
Problem solved. Should have been replaced with
:
$new_content .= $content[$i];
if($i < count($content)-1) $new_content .= "</p>";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question