I get a lot of questions about how to add theme content to only single posts. (Versus pages, the home page, etc.)
Again, I’m using the Thesis theme framework on most of my websites and will go over how to bend Thesis to your will and also I’ll give an easy way to add to any theme, Thesis or not.
I’ll give two simple ways to do this (using custom_functions.php and also with the OpenHook plugin). I’ll even throw in a bonus for people using any theme at all.
Add A Custom Function
This method assumes you’re comfortable modifying your custom_functions.php file. I recommend testing this on a local testing machine and not your live site since you can render your site unconscious (temporarily) if you goof up a bracket. Just copy the following PHP code snippet and paste it at the bottom of your custom_function.php file. Make sure the PHP brackets are not closed or you’ll bork up your site.
function do_in_post_only() {
if (is_single()) { /*don't show on the home page*/
?>
<h3>Test</h3>
<!--do something useful here-->
<?php}
}
add_action('thesis_hook_after_post','do_in_post_only'); /*add after the post content*/
You can see that it’s pretty simple. If you want it in a different location on the post then find where you want it to go, get the hook name and replace the text in red above with your hook name and it’ll be displayed in the location. Need a clue to some of the hook names? You can find a listing of all Thesis hooks and a visual guide is also available if that’s not clear.
Using OpenHook
A plugin I use frequently to make things even simpler (yes, it gets even easier) is Rick Beckman’s Thesis OpenHook plugin.
I won’t go into the details on the plugin, but if you like things easy, then this is for you.
To recreate what we did above, all we need to do is navigate to the plugin page under Appearance->Thesis OpenHook. Find the appropriate hook box (here we will use After Post hook section) and drop in the following PHP code. Check the box to ‘Execute PHP on this hook’ and voila, the same fascinating result is displayed.
For Non-Thesis Themes
Again, I have to jab. If you aren’t using Thesis, why not? Life is too short to hack poorly written themes.
That being said, you can do this but it will require a) working in PHP (just cut & paste, so it’s not very difficult) and b) some trial and error depending on how your theme is designed.
You need to access the PHP files which you can do either through FTP or through the WordPress Admin interface by going to Appearance->Editor. Once there you’ll probably find a file called single.php or post.php or some variant of the two. Edit that file and play around where you want your text/image/whatever to display and paste in whatever PHP functions or HTML code you want. Save the file and that should be it. Again, I’m short cutting here because there are many ways to do it and I can’t cover them all. Just experiment.
There you have it, a simple way to add some text or function only to your single post pages. Let me know how it goes in the comments.
Did you enjoy this article? Get more of it...
Subscribe:
If want to get more stuff like this, subscribe now and you'll receive the latest updates instantly by email.
Share It:
Share this post with your network on one of these popular sites:
Follow:
Follow Jim (@jimfmunro) on Twitter here.
Become A Fan:
Become a Fan of Jim on Facebook.

