Post Tags

WordPress

Pretty posts

You may have seen that, in many WordPress blog post content is advertising. Most of the ads are the first or second paragraph. Whose are new they try to add it into there post one by one. If the posts are plenty of good quality, then it is almost impossible.

Sometimes the user will have to change again to the advertiser. To remove advertiser's one by one from the old posts it's very difficult. So for those who are new to WordPress today, I show them how to add ads to the specific paragraph.

This can be done very easily by using the plugins. There are some of pretty good plugin for this. Try to search it on google. But remember if you use many plugin in your site, that's not good for SEO. So, I will show you how easily this can be done without plugins. The easiest method is shown here:

Open your theme's functions.php file and paste the following code.
<?php
    //Insert ads after second paragraph of single post content.
    add_filter( 'the_content', 'prefix_insert_post_ads' );
    function prefix_insert_post_ads( $content ) {
        $ad_code = '<div>Ads code here</div>';
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_after_paragraph( $ad_code, 2, $content );
        }
        return $content;
    }
    // Parent Function that makes the magic happen
    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 );
    }
?> 
To add advertising code just replace Ads code here with your advertising code. 2 is meant that the ad will be shown after the second paragraph.You can change it's value as your wish.

I hope the future will be to come up with any new tips. If it's help you, you will certainly tell other to shares. If you have any comments or would like to suggest something to you, you can safely comment below. Thanks for your valuable suggestion and take the time to read this tips.