Jump to content

CRUSO

New Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by CRUSO

  1. The function bellow add ads between paragraphs. I'm trying to make it to count a blockqoute as a single paragraph, regardless of how many paragraphs are inside that blockqoute.

    I wasn't able to find the implementation for this. Can anyone provide me a bit of help? I suspect that the hint is to use the DOMDocument() method, but i'm new to php and i don't know exactly how to do it.

    add_filter( 'the_content', 'add_ads_to_content' );
    
    function add_ads_to_content( $content ) {
    
        $ads = array(
            2 =>  'ad code 1', // paragraph_id => ad_code
            4 => 'ad code 2', // paragraph_id => ad_code
            6 => 'ad code 3' // paragraph_id => ad_code
        );
    
        if ( is_single() && ! is_admin() ) {
            foreach ($ads as $paragraph_id => $ad_code) {
                $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $content );
            }
        }
    
        return $content;
    }
    
    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 );
    }

     

  2. I use a function that add ads after X number of paragraphs. I tried to modify it to ignore paragraphs that have <span> inside them but it doesnt work and i get an error.

     

    add_filter( 'the_content', 'add_ads_to_content' );
    
    function add_ads_to_content( $content ) {
    
        $ads = array(
            2 =>  'ad code 1', // paragraph_id => ad_code
            4 => 'ad code 2', // paragraph_id => ad_code
            6 => 'ad code 3' // paragraph_id => ad_code
        );
    
        if ( is_single() && ! is_admin() ) {
            foreach ($ads as $paragraph_id => $ad_code) {
                $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $content );
            }
        }
    
        return $content;
    }
    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        foreach ($paragraphs as $index => $paragraph) {
        
           if( !preg_match( '~<(?:span)[ >]~', $paragraph )) {  
    
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
            if ( $paragraph_id == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
          }
        }
        return implode( '', $paragraphs );
    }

     

  3. Hello guys! I use a function that inserts an ad after X number of paragraphs. I want everything that is between
    [blockquote] and [/blockquote] to be counted only as 1 paragraphs despite what is inside [blockquote]. I cant really find a solution.

     

    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 ( ( ($index + 1) % $paragraph_id ) == 0 ) {
                $paragraphs[$index] .= $insertion;
            }
          
        }
    
        return implode( '', $paragraphs );
    
    }

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.