Posted on 13 April, 2009 By 54 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:

Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter.




web hosting by media temple

54 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 badge

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.

Recent Comments

    • Victoria12: Wow! This are so cute. It is so much special when our kids do it on their own and give it us as a surprise gift.. So tou...
    • Catherine: I hope that the page owners have a plan for the transition. One concern is that Facebook doesn’t have a great method of ...
    • harm: You no longer can "Add to My Page" This used to be simple, using the facebook UI. Unfortunately facebook removed thi...
    • Phil Derksen: I was referencing plugin #1 in the list btw. :)...
    • Phil Derksen: Thanks for mentioning my Pin It button plugin. I'd love to hear what people think of it and what they'd like to see adde...

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