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.
Rick says
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
Kim Woodbridge says
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.
Natural says
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
Kim Woodbridge says
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.
Vered - MomGrind says
“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
Kim Woodbridge says
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. ;-)
Mike Nichols says
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
Kim Woodbridge says
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. :-)
Dennis Edell says
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?
Kim Woodbridge says
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.
Dennis Edell says
Cool I think mines the same way.
Dennis Edell´s last blog post – Blog Readers or Twitter Followers – What’s YOUR Preference?
Natural says
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
Kikolani says
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
Kim Woodbridge says
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
smashill says
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 :)
Kim Woodbridge says
Hi – Thanks! My goal was to make it easy to understand and it seems that I have succeeded.
Chinese Girl says
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
Kim Woodbridge says
Hi – Very cool. When you start to learn more about WordPress it’s amazing how much can be done with it.
Ajith Edassery says
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
Kim Woodbridge says
Hi Ajith – The more I use it, the more I love it. It is SO useful :-)
Nihar says
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
Kim Woodbridge says
Hi Nihar – I think I might use it to only show top commentators and maybe the blogroll on the front page.
Rotem says
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?
Kim Woodbridge says
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
Laurence says
Nice I was looking for this for hours now…..
Kim Woodbridge says
Hi Laurence – Life would be SO much easier if google would just give me the #1 spot in search results ;-)
Srikanth Sundaram says
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??
Kim Woodbridge says
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.
Srikanth Sundaram says
I’ll mail you on any wodpress issues Kim… Again thanks a lot …
AntonRSA says
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 =-.
Kim Woodbridge says
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/
Diego Lozano says
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!
Kim Woodbridge says
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.
Daniel says
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
Kim Woodbridge says
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.
Chintal says
Can we use the same code for post?? and also.. you have not mentioned where to use this code?? in page.php or post.php if i want to try with post.. and also.. is there any plugin to manage this stuff.. Just curious to know the answers of it.. Thanks for the article. It gave much information on the same time it raised this many question. I will be thankful, if you answer my query.
Kim Woodbridge says
I used the code in the sidebar. You can use the same code for a post as a second loop. You would need to edit the template file where you wanted this to be located.
There is a plugin called Widget Logic that might be useful to you. You create your widgets and then with Widget Logic can set which pages they appear on.
http://wordpress.org/extend/plugins/widget-logic/
I don’t know of a plugin that will add conditional code to a post or page.
Chintal says
Thanks for the information. After i read your article, i was researching more on internet and I found this wonderful plugin. I hope this will benefit readers like me reading this post in near future. This plugin let you create multiple sidebar and then we can assign post/page to it.
http://wordpress.org/extend/plugins/sidebar-generator/
Thanks once again. I was searching such plugin for the past few weeks. And only after reading your article. I got the basic keywords which did the magic.
Kim Woodbridge says
Hi Chintal – Great! And thanks for the info about that plugin – it looks
really useful.
keith says
I’ve got this partially working in test environment on my server behind firewall. problem is I’m trying to use the home page as the variable page. If the showing the home page, I want to display a specific graphic in the header. If not showing, then I don’t want to display any image.
I can get this to work on other pages, but not the home page, because it is the landing page and doesn’t display a page name in the url. It is simply
http://www.example.com
It doesn’t say example.com/home
BTW, great tutorial. I wonder why more blogs don’t use this it’s such a great way to customize a wordpress site.
Kim Woodbridge says
Hi Keith
Use is_home instead of is_page
http://codex.wordpress.org/Function_Reference/is_home
keith says
Thanks Kim! I couldn’t get “is_home” to work for me, but you got me going down the right path. This is how I ended up solving it:
It works on my test platform, but I haven’t tested it across browsers. Mac only now.
keith says
Opps: I couldn’t get the php code referenced above through the comment filter.
<!–
–>
essentially, I used “is_front_page” with an if-else statement
Kim Woodbridge says
Hi Keith – Sorry the code didn’t come through but I’m glad is_front_page worked for you.
Mike says
Great Article – Thanks!
This plugin that I just wrote also enables you to publish content in your sidebar that is specific to the current post or page being displayed.
Kim Woodbridge says
Hi Mike – Thanks for the info – I removed a couple of the links in the comment.
Mike says
Another suggestion – The Graceful Sidebar Plugin creates a widget that enables you to use custom fields in a post or page that display in the sidebar. You can use it to display custom html or a simple message. Enjoy!
Kim Woodbridge says
Thanks Mike – I’m definitely going to try it out.
Matt says
Can you put more php if statements after the “else” argument?
Sincerely,
Matt
Kim Woodbridge says
Hi Matt,
Yes, you can but you would want to use elseif rather than else.
There are examples on this page starting with snippet 3
http://codex.wordpress.org/Conditional_Tags
Ryan Burnett says
Works perfectly! I’d been searching for a way to do this when I finally found your page by Googling “wordpress is_page sidebar.” Thank you!
Kim Woodbridge says
Hi Ryan – Thanks! I’m glad the article was useful.
David says
just what i was looking for thanks so much
Kim Woodbridge says
Great – I’m glad it helped.