Sidebar Widgets Show Only On Single Posts/Pages

I was playing around with the Similar Posts Wordpress plugin and I wanted to have it show on the sidebar when it was a single post or page. Now I’ve done something similar before, but it was only to display text:
if (is_single()){
echo "This Entry This is the permanent link for the entry ";
echo the_title();
echo "This post has been filed to ";
UTW_ShowTagsForCurrentPost("commalist");
echo ".";
echo "Related entries:";
UTW_ShowRelatedPostsForCurrentPost("posthtmllist");
echo "
";
}

But the thing was, I had no idea how to do this when I wanted a php code to show up when it was a single post. i knew I’d have to use the is_single() Wordpress conditional tag, but I didn’t know how to implement that with the similar post code:
After playing around with various combinations, this is my working result:
<?php if (is_single()): ?>
<?php similar_posts(); ?>
<? php endif; ?>

Of course, you can replace the is_single() with any other Wordpress conditional tags and replace the <? php similar_posts(); ?> with another php snippet. Just make sure you always add the <? php endif; ?> at the end.

I’m really no expert in php, but this solution worked for me, so if anyone has a better method of getting a php snippet to show up in single Wordpress posts, please share with me! Thanks!

share and bookmark this entry: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Google
  • Live
  • Ma.gnolia
  • Pownce
  • StumbleUpon
  • Technorati
  • TwitThis
  • YahooMyWeb
  • blogmarks
  • Facebook
  • Blogsvine
  • MisterWong
  • NewsVine
  • Propeller
  • RawSugar
  • Reddit
  • Simpy
  • Slashdot
  • Spurl
  • Netvouz
  • E-mail this story to a friend!

Filed under: Coding, Tips & Tricks on May 13th, 2008

Leave a comment

Please read my comment policy before posting. Thank you :)


Comments links could be nofollow free.