R
R
Ruslan Banochkin2014-11-05 13:04:46
HTML
Ruslan Banochkin, 2014-11-05 13:04:46

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;
  }

Everything seems to work, but here's the nuance - at the end of the article, the "p" tag is once again added.
You can see it here: ubuntovod.ru/ubuntu-news/ubuntu-14-10.html .
Well, here's a demo in the code itself:
da52ac29bd3d4c569f8be2994b4c2944.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Banochkin, 2014-11-05
@Sk8er

Problem solved. Should have been replaced with
:

$new_content .= $content[$i];
if($i < count($content)-1) $new_content .= "</p>";

Thanks for the solution to the author of this cozy blog: fe-nix.ru . (:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question