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

      • Kim Woodbridge: HI Dennis - That's exactly right - it's like a macro for WordPress. Don't the Office programs still use them?...
      • Dennis Edell: Anyone needing to ask what a macro is, please do it privately...I hate looking so old in public. LOL! .-= Dennis Edell´s...
      • Kim Woodbridge: Hi Juncai - There isn't any mention of the alt tag in the fbml documentation for the share tag. I tested it by adding i...
      • Dennis Edell: The commonly used link is awesome. Kinda like a modern day macro, yes? .-= Dennis Edell´s last blog .....
      • Kim Woodbridge: Hi Shirley - I would be happy to answer your questions but I don't understand them. Are you referring to the Facebook s...
      • Kim Woodbridge: Hi Dennis - I could never park a car like that - even lining it up with the door in order to get into the house :-)...
      • Kim Woodbridge: Hi Carla - I didn't mean to sound snippy - I am a little irresponsible about it. I could get ok insurance if I tried h...
      • Kim Woodbridge: Hi Jamie - Thank you so much! And I'm really glad this helped you get the video on your fan page :-)...