By using Elementorforum.com’s services you agree to our Cookies Use and Data Transfer outside the EU.
We and our partners operate globally and use cookies, including for analytics, personalisation, ads and Newsletters.

  • Join the Best Wordpress and Elementor Support forum

    Provide or get advice on everything Elementor and Wordpress, ask questions, gain confirmation or just become apart of a friendly, like minded community who love Wordpress and Elementor


    Join us!

How to add content in the middle of Blog post text in Elementor

Community

Community

Administrator
Staff member
This question has come up a few times, we typically do this using a combination of code snippets and Elementor shortcodes, but obviously you can adapt this to work with other forms of code insert.

So we create sections in elementor section builder and design a nice block, for example maybe a posts block section to show related posts

Screenshot 2024-12-05 at 09-21-37 How To Live Off Grid In The UK - Property Help UK.png


Then once we have the shortcode for the section we wish to insert, we add the shortcodes to this function:

PHP:
function insert_random_shortcode_every_fourth_fifteenth_paragraph($content){if (is_singular('post')){$shortcodes = array('[elementor-template id="1562"]', '[elementor-template id="1562"]', '[elementor-template id="1562"]');
        $paragraphs = explode("</p>", $content);
        $new_content = '';
        $paragraph_counter = 0;
        foreach ($paragraphs as $paragraph){$new_content .= $paragraph;
            $paragraph_counter++;
            // If this is the 4th paragraph or a multiple of 15 after that, add a random shortcode
            if ($paragraph_counter == 4 || ($paragraph_counter > 4 && ($paragraph_counter - 4) % 15 == 0)){$random_shortcode = $shortcodes[array_rand($shortcodes)];
                $new_content .= $random_shortcode;
            }
            // Add closing paragraph tag (it was removed in explode function)
            $new_content .= '</p>';
        }
        return $new_content;
    }
    else {
        // If not a single post, return the content unmodified
        return $content;
    }
}
add_filter('the_content', 'insert_random_shortcode_every_fourth_fifteenth_paragraph');

Which adds these sections to every 4th paragraph.

If you wanted to use code, instead of shortcodes, for example Google adsense. THen you would use something similar to this ( this is untested )

PHP:
function insert_adsense_every_fourth_fifteenth_paragraph($content){if (is_singular('post')){$adsense_codes = array(
            '<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>',
          '<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>'
        );
        $paragraphs = explode("</p>", $content);
        $new_content = '';
        $paragraph_counter = 0;
        foreach ($paragraphs as $paragraph){$new_content .= $paragraph;
            $paragraph_counter++;
            // If this is the 4th paragraph or a multiple of 15 after that, add a random AdSense code
            if ($paragraph_counter == 4 || ($paragraph_counter > 4 && ($paragraph_counter - 4) % 15 == 0)){$random_adsense_code = $adsense_codes[array_rand($adsense_codes)];
                $new_content .= $random_adsense_code;
            }
            // Add closing paragraph tag (it was removed in explode function)
            $new_content .= '</p>';
        }
        return $new_content;
    } else {
        // If not a single post, return the content unmodified
        return $content;
    }
}
add_filter('the_content', 'insert_adsense_every_fourth_fifteenth_paragraph');
 
Elementor Services Elementor Services

Latest Resources

Other Elementor Resources

elementor official
Top