Add Anything to the End of Posts

Show anything you want after every post

There are many reasons that you might want to add things to the end of every single post. For example, you might want to add an author’s info box, a little blurb about your email newsletter or RSS feed, or you may just want to display an ad after your content.

Whatever the reason, it’s much simpler than you think. There’s two ways of doing it; the passable way and the best way. Obviously, i’m only going to discuss the best way in detail, but if you wanted to do it the passable way, you just need to edit your theme’s single.php file and insert whatever code you want to display below the loop. It has the downside of being overwritten when you upgrade your theme and not being transferrable to other themes automatically, which is why I recommend the better way.

The best way

As with most things in WordPress, the best way to achieve this is to create a small function and put it into your functionality plugin. In this example, I’m going to add a box after the content advertising my newsletter, but I’m going to include a conditional statement so that it is only displayed when viewing a single blog post (i.e. it won’t appear in archives or the RSS feed).

Here’s the code that you need to work with:

As you can see, the piece you need to be concerned with is between the two single quotes. This is where you can put whatever you want to appear at the end of your posts. You can use PHP if you need to by opening and closing the PHP tags multiple times. For example, the snippet below switches several times between PHP markup and HTML markup:

Be creative! What have you used this little trick for?

15 thoughts on “Add Anything to the End of Posts”

  1. Larissa says:

    Great tip! Maybe I really should add something, perhaps some “share” options, that would be good for the traffic :) Thanks!

  2. Richard says:

    Awesome tipe! I often have to turn to a plugin to do this kind of functionality, but going that route lacks the customizability of doing it yourself.

    1. Indeed. And it’s so simple to do, that it’s much better to do it in your own functionality plugin, rather than going with a plugin.

  3. Shyxter says:

    Hi Dave! I hope you had a merry Christmas :) This post is very explanatory. I admit that just like most people, I rely on plug-ins to do many jobs for my website. Somehow I feel afraid to do things on my own because I might mess things up. But what you have shared here is quite simple and so maybe I can really do this. I think one of my New Year resolutions now is to not depend so much on plug-ins. I should start learning some good coding for my website. Thanks Dave! Really appreciate your post.

    1. With so many resources of snippets available, you don’t really have to learn coding – you just have to know how to install these snippets. Just create a functionality plugin, activate it and paste your snippet in the plugin. It’s that simple. All this functionality is at your fingertips, if you’ll just take the initial step of creating the plugin. It’ll take you 15 minutes max.

  4. Naomi says:

    This look greats! A question, tho: how can we modify the functionality plugin for two lines of code? Such as, both a ‘Please subscribe to our newsletter’ and a newsletter button below it?

    Thanks for any help :)

    1. Naomi,

      If you want to add more content to the bottom of your posts, you can just keep entering $content .= lines and it will keep adding them to the bottom of the page. I.e.

      $content .= 'Enter the first thing to add after the post here.';
      $content .= 'Use either a < p > tag or a carriage return (\n) to start this next line below the previous one';

  5. Great stuff, thank you very much!

    One question, though. For me it worked fine with text, but when trying to add a Facebook share button, I’m getting “syntax error, unexpected T_STRING”.

    This is the code:

    (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); = id;
    js.src = “//″;
    fjs.parentNode.insertBefore(js, fjs);
    }(document, ‘script’, ‘facebook-jssdk’));
    <div class="fb-share-button" data-href="” data-type=”button_count”>

    Any ideas what needs to be changed?

    Thanks in advance!

    1. Yes, you’ve got a big mixture of PHP, HTML and JavaScript and you need to transition between languages correctly. You need to make sure that if you use single quotes to denote the code to be added at the end of posts that there are no single quotes in the code itself (or that you escape them).

  6. Mark says:

    When I use this method, my information is added at the top of the post. I don’t know if it’s my theme but I can’t find a way around it except directly in a custom single-mysingle.php file.

    1. If you copied the format of this code, whatever element you’re adding to $content will be shown directly after the post content, unless another function in your theme or a plugin is interfering and further modifying the_content. You could try changing the 0 (priority) to 9999 to make your change happen last, which should overcome that issue, if that is the issue.

  7. Mark says:

    Thanks Dave. I think you’re right that it’s something to do with the theme.

  8. bensinclair970077 says:

    Changing the priority did not work for me, when I changed it to 9999 the $content went to the very bottom of my post under another plugin. When I first used this function it the text stays at the bottom of the post (where i want it), but the php elements “bloginfo(‘name’)” etc show at the top of my content…. its like WordPress is separating the html from the php…

    I’ve been stuck on this for hours lol!

  9. hello.
    nice little code.
    work perfect.
    Now what do i have to change to have the same in pages?
    Thanks a lot

    1. Just change is_single to is_page. To show on posts and pages, change is_single to is_singular.

Leave a Reply