Posted on 13 April, 2009 By Kim Woodbridge 35 Comments

How to Conditionally Display WordPress Sidebar Content with is_page

 
Share

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. You can also connect with my Facebook page or follow me on Twitter.



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

    Trackbacks/Pingbacks
    1. Section Specific Slideshow WordPress
    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> .

    CommentLuv Enabled

    Recent Comments

      • Ari Herzog: Agreed on the updates/inbox snafu. I rarely see page updates and I know where they are! .-= Ari Herzog´s last blog .....
      • jayson: Kim, Love the themes. I am looking to update the theme on my site to something more appropriate. My site is all abou...
      • vered: "I wish more people would use this. I think it’s silly when I get updates for things occuring in Kansas and California –...
      • Dennis Edell @ Direct Sales Marketing: I was speaking generally, not specifically this month. how exactly do you work things? .-= Dennis Edell @ Direct Sales M...
      • Lucy Beer: Unfortunately I think next to no-one actually sees those "Updates". They're buried in a subsection of the inbox and I do...
      • Kim Woodbridge: Hi Marbella - Thanks! I had a great time....
      • Kim Woodbridge: Hi Julie - I'll probably try kayaking again next summer but I don't know if I will become much of a kayaker - I had a di...
      • Kim Woodbridge: Hi Vered - If you ever get the chance, you should go. It's like a different world - it's so quiet and peaceful....

    About

    Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

    Wordpress Services

    • Installation, upgrades and maintenance
    • Conversion of existing html and css templates
    • Theme and plugin recommendations
    • CSS customizations
    • Troubleshooting and tweaks for unique situations
    • Customization for individual blogging goals and needs
    • Training and advice