How to Add Extra Content Before or After Each Post in WordPress

February 5, 2014 — 3 Comments

In this article, we will show you about how we add extra content before or after each post in WordPress easily. It’s really easy to add an extra content of your desire at the end as well as at the starting of the posts in WordPress.

There are couple of ways that you can do it, but the one that we are going to show you guys is really easy to understand and actually makes sense.

Let me tell you that in this article we are going to use a filter hook (‘the_content‘) that will actually allow use to add extra content at the end as well as at the starting of each post.

Here is the complete code:

function ex_extra_content($content){
if(is_single()) {

$content .= '<strong>Thank you for Reading.. </strong>';
}
return $content;
}

add_filter('the_content','ex_extra_content');

Let me tell you that what we have just done in the above  code.

So, first of all we created a function with the help of:

function some_name_of_function(){}

Make sure that your function name should be unique so it will not conflict with the WordPress and other plugins’ functions because while loading the site WordPress execute every single function of a website if it find one or more functions of the same name then one of the function will be loaded but the other one won’t. So, name your plugin functions clearly.

And then after creating the unique function we added the parameter of WordPress core $content to let WordPress know about where we want our function to work.

function some_name_of_function($content){}

Afterwards, we specify the WordPress about where we want our function to work by using the conditional tag of WordPress “is_single()” which will further tell WordPress we want to execute our function only on Posts. And if we use “is_singular” conditional tag then the function will be executed on both Pages, Posts but as we want to add extra content (such as “Thank you for Reading…”) only on posts we will use “is_single()” conditional tag simply.

if(is_single()) {

Now,  after using conditional, we simply concatenate the current post $content + extra content by using the following line of code:

$content .= '<strong>Thank you for Reading.. </strong>';

After doing this, we tell WordPress to return the only content on the webpage if something went wrong by using return $content;.

Finally, we activate our function by using the filter hook “the_content” where ex_extra_content is our custom function that we want to work on each single post page:

add_filter('the_content','ex_extra_content');

3 responses to How to Add Extra Content Before or After Each Post in WordPress

  1. I have done this but my site still not showing this. Can you help?

    • MuhammadHaroon March 13, 2014 at 1:56 pm

      Hey @Julia Richard,

      What is the error that you are facing after implementing this article. Can you please add a screenshot of the error that is coming up next after using this guide on your WordPress blog.

  2. Thanks for the code.i am littlebit slow in coding.i have one question in mind?? is there is any plugin available which do this work for me…

Leave a Reply

*

Text formatting is available via select HTML. <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>