Posted on 13 April, 2009 By Kim Woodbridge 35 Comments

How to Conditionally Display WordPress Sidebar Content with is_page

2009_summer 293 (by kwbridge)

In a non-widgetized sidebar you may have content that you only want to show on certain pages but not on others. I started thinking about this after my (Anti) Social WordPress Spring Cleaning article where Ajith mentioned that on my portfolio page I was duplicating the testimonials. I had the testimonials listed with each project and in the sidebar.

So, I decided to use conditional code to only display the sidebar testimonials on particular pages. This part of my site uses a different sidebar than the blog. You can see the testimonials on the About, Services or Contact pages but not on the Portfolio page.

Here is the code I use to do this. It’s conditional code – if this do this, if not do something else – that checks to see what page is being displayed. It shows the testimonials or it doesn’t.

<?php
if (is_page(array('contact','about','services'))) { ;?>

list of testimonials here

<?php } ?>

The is_page (array … is checking to see for the three pages listed and shows the testimonials, if we are on one of those pages. You can use the page name or the page ID in the code.

If I wanted to show something in place of the testimonials I would include the else code.

<?php
if (is_page(array('contact','about','services'))) { ;?>

list of testimonials here

<?php } else { ?>

alternative code here

<?php } ?>

That’s really all there is to it. You can read more about WordPress conditional tags in the codex. The more time I spend reading this part of the codex the more it starts to make sense to me.


Related Posts:
  • Random WordPress Quotes Without A Plugin
  • (Anti) Social-Lists 5/24/09
  • Using Thumbnails for Excerpts Plugin with Query Posts to Make a Custom Sidebar Block in WordPress
  • 8 WordPress Spring Cleaning Tips: Backups and More
  • Using Conditionals with WordPress 2.7 Sticky Posts
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    35 Responses to “How to Conditionally Display WordPress Sidebar Content with is_page”

    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • softize.net: Yeah, I just upgraded my site in just 5 minutes and it was very very easy to do, thanks to this post also. Sometimes we ...
      • Kim Woodbridge: Hi - Thanks! It still works without any problems :-) I used to use my guide to do upgrades but have done so many now t...
      • Kim Woodbridge: Hi Julie - Tweetdeck does make it a lot easier :-)...
      • softize.net: Hi Kim, I must say this guide was excellent for everyone. You give detailed instructions and security notices that ever...
      • Julie Walraven | Resume Services: This post stuck with me for a long time... I finally listened awhile back and dropped the connection. Then I realized in...
      • Kim Woodbridge: Hi Keith - I originally joined Facebook to find an old friend. I never expected to end up liking it as much as I do....
      • Kim Woodbridge: Hi Rajat - Thanks for the info! I'll try it out as soon as I get the chance....
      • Kim Woodbridge: Hi Timothy - I'm glad you agree - it seems that more people are using it. I'm not seeing quite as many FB updates that ...