Automatically insert custom code after the “more” tag

<–more–> tag is a simple, yet very interesting WordPress feature. It allows you to divide content and to show only an excerpt of your article to readers. By doing that, you can give your visitors a list of posts which they can easily browse and where they can decide which article is worth reading. If a user is interested enough in reading the entire article, <–more–> tag will give him a link to the rest of the content.

Sometimes, if you have interested people into checking your post, you will want to display an advertisement or maybe some extra content right after the tag. This may be a great location for such content because a visitor has already read the excerpt and his attention will automatically flow right after the last sentence of the excerpt. It can be the rest of your article or it can be an advertisement, a link or a list of most commented posts in a category, just to mention few examples.

In this article, we’re about to show you the code which will allow you to do that automatically. Instead of copy/pasting a shortcode or the entire code after each tag, you can do it once and let your WordPress take care of code implementation in the future.

OK, now that you have decided that you need something inserted right after the “more” tag, let’s see how to do that.

Automatically insert code after <–more–> tag:

  1. Open functions.php file from the theme you’re using
  2. Copy and paste the following code:
  3. add_filter('the_content', 'adds_block'); 
    function adds_block($content){
    if (is_single())
    global $post;
    $thePostID = $post->ID;
    $new_insert = '<span id="more-' .$thePostID.'"></span>' ;
    return str_replace($new_insert, my_ad(), $content);
    }
    function my_ad(){
    return ' Your Ad Code ';
    }
  4. Add custom code, a text, image, advertisement or anything you like after the more tag. You should place your code in line #12, between the single apostrophes:
  5. return ' Your Ad Code ';
  6. Save changes

You’re done! After you have saved the changes, open any post which has more tag or create a new one just to test the function. If your custom code is correct, you should see that extra content or advertisement displayed right after more tag.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *