Support » Developing with WordPress » How do I remove the_content and is_single advertisement from bbpress?

  • Resolved mllapan

    (@mllapan)


    I have this function to display advertisement after first paragprah in posts:

    add_filter( 'the_content', 'post_ads_1_paragraph' );
     
    function post_ads_1_paragraph( $content ) {
        $ad_code = '<div class="advert" style="display: none;">Reklama1</div>';
     
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_1nd_paragraph( $ad_code, 1, $content );
        }
         
        return $content;
    }
      
    function prefix_insert_after_1nd_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 );
    }

    The problem is that this function is somehow hooked to bbpress forum listing (forum description) to, and shows after forum description.
    So how can I remove it from bbpress so it affects only posts?

    See word “reklama1” after each forum description: https://mllapan.com/forumi/forum/magazin/

    • This topic was modified 1 year, 6 months ago by mllapan.
Viewing 1 replies (of 1 total)
  • Thread Starter mllapan

    (@mllapan)

    Solved!

    add_filter( 'the_content', 'post_ads_1_paragraph' );
     
    function post_ads_1_paragraph( $content ) {
        $ad_code = '<div class="advert" style="display: none;">Reklama1</div>';
     
        if ( is_single() && ! is_admin() && ! is_bbpress() ) {
            return prefix_insert_after_1nd_paragraph( $ad_code, 1, $content );
        }
    	
        return $content;
    }
      
    function prefix_insert_after_1nd_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 );
    }
    • This reply was modified 1 year, 6 months ago by mllapan.
Viewing 1 replies (of 1 total)
  • The topic ‘How do I remove the_content and is_single advertisement from bbpress?’ is closed to new replies.