Connect with us

How to place Ads within your post content in WordPress

How to place Ads within your post content in WordPress

Blogging

How to place Ads within your post content in WordPress

I’m pretty sure that you’ve seen sites that have ads in between their post and you keep on wondering how they did it. These ads might either be before the post or after the post. Sometimes, they seem to appear before after the first paragraph or even the second paragraph, which looks cool to most beginners.

Most bloggers wonder if the codes are inserted manually when the articles are written or if there happens to be a special code for it. Most times, when bloggers try including Ads in their posts, they either try adding the code manually or they insert ads above or below their posts using one of those ad placement plugins.

Well, I’ve been adding ads in some major posts after the second paragraph, and I’m sure that you would want to know about it, so, I’m going to share it today.

Simply install a plugin called Insert Post Ads. Upon activation, the plugin will automatically create its own menu in the dashboard called – Post Adverts.How to place Ads within your post content in WordPress

On the dashboard, click on Post Adverts>>Add New Post. On the next window, simply ad the name of your ad, this serves as a means of identification and code. Select the paragraph that you will like this ad to be displayed, once you are done, simply click Publish.

How to place Ads within your post content in WordPressNow, you will need to go to the Post Adverts menu under the dashboard, click on Settings, select where you would like the ad to be displayed such as pagesposts or custom posts.

If you are the type that doesn’t like plugins, you can use the functions.php to do it. Simply open your theme’s functions.php and paste the following code listed below in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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 goes 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 your ad code, edit $ad_code value, where it is written Ads code goes here in line 9. Once that is all done and dusted, you can now change what paragraph number you would like your ad to be displayed. Simply change the number on line twelve to any paragraph number of your choice.

We hope this article helped you in placing your ads in between your post content in WordPress, kindly share your experience using the comment box.

Continue Reading
Advertisement

Get the latest technology news, games, apps, web tutorials, phone specifications, blogging tutorials, affiliate programs, computer tips and so on

Click to comment

Leave a Reply

Your email address will not be published.

More in Blogging

To Top