How to Conditionally Display WordPress Sidebar Content with is_page
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:









April 13, 2009 at 8:58 am
I like it…thanks for posting this information in a way that non-coders like me can understand!
Rick´s last blog post – Rick Answers his Email Videocast – 040
April 13, 2009 at 11:52 am
Hi Rick – Thanks! I try to explain it so everyone can understand. So many articles I find assume a certain level of knowledge and they can be hard to follow.
April 13, 2009 at 10:24 am
so this code can be used on any php page? not just wordpress? i have an option to add my own php code to a section in my forum, but i don’t know what code could go there. i guess the above would be an example. i can fool around a little in php, but i really don’t understand it. i have wondered though what code i could possible use for that section. hmm, maybe i’ll go play and see what happens.
this comment might not make sense but i think you helped me understand something a little better. :)
Natural´s last blog post – My Two Left Feet
April 13, 2009 at 11:57 am
Hi Natural – The code is WordPress specific. WordPress uses a modified version of PHP.
In PHP you might want to look at file_get_contents or file_exists.
What do you want to display in the code block? That would help determine what php code to use. I know a little.
April 13, 2009 at 4:22 pm
“The more time I spend reading this part of the codex the more it starts to make sense to me.”
What can I say… it’s great that people like you exist!
Vered – MomGrind´s last blog post – Skiing In Lake Tahoe, California
April 13, 2009 at 4:24 pm
Hi Vered – LOL – I’ve been reading that part of the codex on and off for a couple of years. I guess if I read it enough times it finally makes sense to me. ;-)
April 13, 2009 at 4:32 pm
Kim, thanks for this tip! There are a couple of pages that I don’t want sidebars on, and I think this will do the trick. Now all I have to do is to translate it to Thesis-speak!
Mike Nichols´s last blog post – Beyond Zits: Acne and Anxiety Disorders Part 2
April 13, 2009 at 5:02 pm
Hi Mike – Well, I kind of answered you on Twitter but in at least version 1.3 + of Thesis you can use the No Sidebars template on a page by page basis.
I suppose you could wrap the entire sidebar in is_page but then I think you would have a big empty white space. Regardless, it’s still a useful piece of code.
Thanks for the retweet. :-)
April 13, 2009 at 5:12 pm
So this has nothing to do with widget-ready themes?
Dennis Edell´s last blog post – Blog Readers or Twitter Followers – What’s YOUR Preference?
April 13, 2009 at 6:23 pm
Hi Dennis,
It won’t work wrapped around a widget but it can still be used with a widget ready sidebar, if the sidebar also includes code. For example, the top part of the sidebar on this page contains code but where it splits into two parts is widgetized.
April 13, 2009 at 7:00 pm
Cool I think mines the same way.
Dennis Edell´s last blog post – Blog Readers or Twitter Followers – What’s YOUR Preference?
April 13, 2009 at 10:17 pm
oh well then never mind. if it’s WP specific. i’m not sure what i want to add in the block. i just have the OPTION of adding some php code to display something. i’m not sure what code i would want to display…but i understand the scenario.
Natural´s last blog post – My Two Left Feet
April 14, 2009 at 1:29 am
Thank you for sharing this. Just the other day, I thought about doing different sidebar items for different pages. Or actually, different categories, so if a post of mine were in photography, it would show photography interest links, and so on. I’m guessing that kind of thing could be done, assuming my posts were only marked in one category.
~ Kristi
Kikolani´s last blog post – Twitter Lingo & Tips For New Twitter & Facebook Users
April 16, 2009 at 9:41 pm
Hi Kristi – I can’t believe I missed your comment. I even remember thinking about it and starting to reply … geesh.
It would be easier if the posts were only in one category but I don’t think they would have to be. Take a look at in_category rather than is_page – I think that would do what you need.
http://codex.wordpress.org/Template_Tags/in_category
April 14, 2009 at 6:47 am
pefectly simple explanation of a very useful feature. I remember when wordpress designer had his articles about it, was not as easy to understand as this. Good job :)
April 14, 2009 at 9:36 am
Hi – Thanks! My goal was to make it easy to understand and it seems that I have succeeded.
April 14, 2009 at 9:00 pm
Yes, you can do many things with word press which is very flexible.
I am using similar hack to show different sidebars for different category , on anther blog I am using different sidebar according to the authors.
anybody wants me to share it?
Chinese Girl´s last blog post – Photo Hangzhou Mountain Putuo
April 15, 2009 at 12:01 pm
Hi – Very cool. When you start to learn more about WordPress it’s amazing how much can be done with it.
April 21, 2009 at 2:55 am
is_page is almost always there behind the changes that I make to my theme. It’s a pretty useful function :)
Ajith Edassery´s last blog post – Latest innovation from Google Labs – News Timeline
April 21, 2009 at 11:29 am
Hi Ajith – The more I use it, the more I love it. It is SO useful :-)
April 23, 2009 at 2:08 am
I have used it to remove or add nofollow rel tag to top commentators list on my blog.
It is really useful.
Nihar´s last blog post – Find Friends on Social Networking sites using Identity
April 23, 2009 at 8:40 am
Hi Nihar – I think I might use it to only show top commentators and maybe the blogroll on the front page.
May 18, 2009 at 4:03 pm
Didn’t work for me. I tried to show an empty sidebar on one of the pages. Instead – it showed the contents of the FOOTER. I have no idea how this could happen.
ANYONE?
Rotem´s last blog post – Is it Sleep Apnea or ‘Just’ Snoring?
May 19, 2009 at 6:49 am
Hi Rotem – All I can think of right now is that the closing php tag was not in the correct location.
There is more information in the WordPress codex.
http://codex.wordpress.org/Conditional_Tags
July 21, 2009 at 1:39 pm
Nice I was looking for this for hours now…..
July 21, 2009 at 8:56 pm
Hi Laurence – Life would be SO much easier if google would just give me the #1 spot in search results ;-)
September 17, 2009 at 3:32 am
Hi Kim… Thanks so much… Im just startin out with wordpress… This really saved a lot of time investment.. You’ve really put it very simple and my sidebars are displayed in every page as exactly as i want them to be :).. This forum certainly deserves no.1 spot rather than 2 on google…. How do I get in touch with u for more queries on wordpress??
September 17, 2009 at 7:52 am
Hi Srikanth – I’m glad the article helped.
lol – yes, google should always give me first ranking ;-)
My email address and contact form are listed in the sidebar.
September 18, 2009 at 12:42 am
I’ll mail you on any wodpress issues Kim… Again thanks a lot …
November 10, 2009 at 10:37 am
Thanks for the code. I’ve been trying to find a plugin that does exactly this as I’m a quite a lazy coder. Why re-invent the wheel? Well anyway, no plugin found but I have found your page with an explanation of this. Implemented it and voilla! It works. Thanks once again.
AntonRSA´s last blog ..Your website is your image to the world
November 10, 2009 at 10:48 am
Hi – There is a plugin that will allow you to control which pages a widget appears on – that might help.
http://wordpress.org/extend/plugins/widget-logic/
January 13, 2010 at 8:51 pm
VERY VERY VERY VERY VERY USEFUL!!! Thanks…. After hours trying this (is_page) thing, I found this page and fix my problem with wordpress. Wow… Thanks!
January 13, 2010 at 9:02 pm
Hi Diego – Great! I’ve struggled with these things to – as soon as I get something to work I write it up in a way that is understandable – as an article I wish I had found when I was struggling.
January 19, 2010 at 7:26 am
Great, but, what if i want that for certain url, and only that.?
ie. http://www.example.com/page1.html
That page use the same template and it’s a php page
January 19, 2010 at 8:47 am
Hi Daniel – If you only want it for one page that you use is_page without the array.
is_page(’services’) or whatever the name of the page is.