Skip to content

Mstoic

  • Home
  • Start Here!
  • Best Deals
  • Facebook Tricks
  • Privacy
  • Resources
  • TOC
  • Tools
    • Web Tools
      • Stop Watch
      • QR Code Generator
      • Adsense Code Converter
      • Blogger Sitemap Generator
      • Difference Between Two Texts
      • Vacation Checklist
      • Twitter Symbols
      • Count Down
    • Investment
      • SIP Calculator
      • Power of Compounding
      • PPF Calculator
      • FD Calculator
  • About Us

Adding Custom Content Before/After Post Content in WordPress

Editorial Team | February 22, 2015February 22, 2015 | Wordpress |

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

  • Turn OFF Autosave In WordPress And Reduce Database Size
  • Google Authenticator for WordPress
  • 4 Ways To Enable Gzip Compression In WordPress
  • 22 Things To Do For A Faster WordPress Website
  • Optimizing Database For WordPress Websites Automatically [Free Plugin]

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.

Previous Post

Display Page Load Time in WordPress Without Plugin

by Editorial Team

Next Post

60% Off On HostGator Hosting

Featured Image

by Editorial Team

Recommended Next
2 Ways to Edit Functions.php File in WordPress

Your Comment Cancel reply

Follow Us

Like us Follow us Follow us

Categories

  • Tricks (93)
  • Facebook (83)
  • Tips (76)
  • Windows (67)
  • Wordpress (39)
  • Android (38)
  • Internet (33)
  • Antivirus (30)
  • How To (29)
  • Webmasters (25)
  • Customization (25)
  • Downloads (21)
  • Utilities (21)
  • Gmail (20)
  • Windows 7 (19)

Related Articles

  • Reverse a String using StringBuilder