• 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) {
    // Check if we are viewing a single post
    if (is_singular('post')) {
        // Define your shortcodes
        $shortcodes = array('[elementor-template id="1562"]', '[elementor-template id="1562"]', '[elementor-template id="1562"]');

        // Split the content into an array of paragraphs
        $paragraphs = explode("</p>", $content);
        $new_content = '';
        $paragraph_counter = 0;

        // Iterate over the paragraphs
        foreach ($paragraphs as $paragraph) {
            // Add paragraph to new content
            $new_content .= $paragraph;

            // Increment the counter
            $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)) {
                // Select a random shortcode
                $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) {
    // Check if we are viewing a single post
    if (is_singular('post')) {
        // Define your AdSense ad codes
        $adsense_codes = array(
            '<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>',
          '<div class="adsense-ad">YOUR ADSENSE CODE 1 HERE</div>'
        );

        // Split the content into an array of paragraphs
        $paragraphs = explode("</p>", $content);
        $new_content = '';
        $paragraph_counter = 0;

        // Iterate over the paragraphs
        foreach ($paragraphs as $paragraph) {
            // Add paragraph to new content
            $new_content .= $paragraph;

            // Increment the counter
            $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)) {
                // Select a random AdSense code
                $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');
 

Latest Resources

Other Elementor Resources

elementor official

Still stuck? Ask the forum

Post your question with a screenshot and a link if you can, and a member will usually reply within a day. Free to join, no sales pitch.

Create a free account
Top