There can be many occasions when you may want to add some content before or after the post content. You may want to add a subscription box, links to your social networks, some affiliate product links, or even social sharing icons. But doing this is not easy, as you will have to install a custom plugin, which I am not a big fan of, or you will have to get your hands dirty with the code. I like editing my template files and adding my custom code snippets.
You theme has a single.php file, which contains all/part of all the content that is displayed on single post pages. Mostly people directly edit this file to add content to single post pages. But WordPress offers a filter to make changes to the post content. The benefit of using filters is that you can place them all in one file which makes them more manageable.
We will be adding all the code snippets in the functions.php file. If you don’t know how to edit that, read this article.
Related Articles
Adding Custom Content Before Post Content
The code snippet below adds the content in blue before the post content on all the single post pages. You can use HTML here.
add_filter('the_content', 'mstoic_prepend');
function mstoic_prepend($content) {
// Check if single post page is being displayed
if (is_single()) {
// Add custom content
$new_content = '<a href="social-network">Social Network Name</a>';
// Append the original post content
$new_content .= $content;
// Return the new content
return $new_content;
} else {
// Not single post page, return the original content
return $content;
}
}
Adding Custom Content After Post Content
The code snippet below adds the content in blue after the post content on all single post pages.
add_filter('the_content', 'mstoic_append');
function mstoic_append($content) {
// Check if single post page is being displayed
if (is_single()) {
// Add the original content
$new_content = $content;
// Append the custom content
$new_content .= '<a href="social-network">Social Network Name</a>';
// Return the new content
return $new_content;
} else {
// Not single post page, return the original content
return $content;
}
}
Just pasting the above code snippets to your theme’s functions.php file will allow you to add content before and after the post content. Make sure you copy whole of the code, making errors in PHP files will prevent your site from loading.