How to Add Ads after Read More Tag in WordPress

ads-after-more-tag

Today, this site finally reaches a new milestone, 500+ page views/day. Even though it’s very low, I decided to add ads in this blog.

This blog uses full content in archive, and I’m breaking long content using manual “read more tag” (not using excerpt).

Note: You can add more tag using the editor toolbar or using <!--more--> in text editor.

I want to add the Ads in all blog post after read more tag, so technically when visitor click the read more, they will see ads on the top of the page.

It’s tricky because when we use the_content filter, WordPress already transform <!--more--> tag into HTML tag, something like <span id="more-1325"></span>. Here’s the code:

I found the code on stack overflow:

/* Filter content */
add_filter( 'the_content', 'my_content_ads' );

/**
 * Add Ads in Content
 */
function my_content_ads( $content ){

    /* Not post, return. */
    if( !is_singular( 'post' ) ){
        return $content;
    }

    /* Ads Code */
    $ads_code = '';

    /**
     * Add ads after more tag.
     * @link http://stackoverflow.com/questions/887447
     */
    $content = preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1"></span>'."\n\n". $ads_code ."\n\n", $content );

    return $content;
}

You need to simply change the $ads_code var with your ads code.

3 Comments

  1. Oliver

    Hi
    What if I wanted to show some ad code after the read more tag, but do it in the post instead? The read more tag location seems like a good location to show the first ad on a post page.

Comments are closed.