Guest post written by Richard Paul
Hi fellow MUsers!
Okay, you are amazing at design or have picked out the perfect theme. You know how to install a plug-in and add widgets, mash-ups, and other gizmos. Some of these you have hard coded into your theme, others you uploaded and enable from the back-end admin area. Maybe you have plans to add a few more, but where the heck will you put them?
Instead of asking, “Where?” to place that new sidebar content, how about asking “When?” Once you grasp onto the idea that you have a ton
of control over “when” something is displayed, you will gain a ton of new dimension in which to design your content. Maybe you are already familiar with the built-in conditional tag functions that will be discussed here, but take a fresh look anyway. This is particularly useful when designing the “home” theme of your network.
By adding a few simple snippets of code to your sidebar(s), you open new doors on design through which you will be allowed access to what I like to call, “The developers time/space continuum.” By “time”, I am of course speaking in relation to “when”, as in “when” your visitor is on a particular page, at “that time” I want to display “this content.”
Let us use a fictitious widget gizmo, what I call a wizmo. The name of our wizmo is “Klicky.” Now say we have the PHP code to add the Klicky wizmo to our theme and want him to display in the sidebar. In this example, we are ready with the code to paste Klicky into the sidebar.php file of our theme.
You right click and paste Klicky exactly where you want him and upload your sidebar.php file to checkout how he looks and make sure he is working properly. Everything seems to be in order but the question is “How important is Klicky anyway?”
Is this new wizmo worth displaying on your homepage? Do you want it to show up when your visitors are reading your latest post? Maybe you only want it to be available on your blog “post” pages and not on your regular “page” pages.
It is entirely up to you to decide how important Klicky is to you and how useful he will be to your visitors. Once you gauge his level of importance, you can wrap his code inside any of the following tags to display him when you think he will be most useful. If Klicky is very important, you might consider placing him on the homepage.
Most of your traffic will enter your website through the front door and therefore most visitors will have a chance to be introduced to Klicky. The question remains, if he has been seen by “most” visitors, do most visitors need to see him again on other pages? If the answer is no, then allow Klicky to display on the homepage only.
Here is how we do that.
<!– Show only on homepage and no other pages or posts –>
<?php if ( is_home() ) : ?> <!– Klicky Begins –>
Klicky Wizmo Code Goes Here!
<?php endif; ?> <!– Klicky Ends –>
OR
For versions 2.5+, the is_home() tag used in the code above has been updated.
If your version of WordPress is 2.5 or above, use the is_front_page() tag instead.
Here is the updated code.
<!– 2.5+ Show only on homepage and no other pages or posts –>
<?php if ( is_front_page() ) : ?> <!– Klicky Begins –>
Klicky Wizmo Code Goes Here!
<?php endif; ?> <!– Klicky Ends –>
Here we want Klicky to display on every page including the homepage, but not display on post pages. By the way, “Post” pages include those that display archives and searches.
Here is the code
<!– Show on every page including homepage – not on post pages –>
<?php if ( is_home() || is_page()
) : ?> <!–
Klicky Begins –>
Klicky Wizmo Code Goes Here!
<?php endif;
?> <!–
Klicky Ends –>
You know how valuable the space is on your homepage. Please don’t overload your visitor with too much content or slow loading flash based ads. Consider putting your heaviest and largest sidebar ads on pages other than the homepage and using smaller and lighter ads on post type pages.
Here is the code
<!– Show only on pages except homepage – not
on post pages –>
<?php if ( is_page() ) : ?>
<!– Klicky Begins –>
Klicky Wizmo Code Goes Here!
<?php endif;
?> <!–
Klicky Ends –>
If we explicitly express that we want to eliminate all the page conditions, then all that is left are the post driven pages. In other words, your blog driven pages. Here we will not display Klicky on our homepage or “page” pages, but show him everywhere else on your blog.
Here is the code
<!– Show only on post pages – not on page pages
–>
<?php if ( !is_home() AND !is_page()
) : ?> <!–
Klicky Begins –>
Klicky Wizmo Code Goes Here!
<?php endif;
?> <!–
Klicky Ends –>
For extra credit, try removing the first exclamation mark (!) in the Show on Post condition above, changing it to is_home(). This will make Klicky display on the homepage and post pages but not on “page” pages.
Placing content outside of any conditional tag parameters will result in that content being displayed on every page AND every post. Hmmmm, “Is there anything that I want to ALWAYS display in my sidebar?” If so, all we have to do is paste the Klicky code anywhere in the sidebar.php file outside of any condition and upload for Klicky to “show always.”
Here is the code
<!– Klicky Begins –>
Klicky Wizmo Code Goes Here!
<!– Klicky Ends –>
Thanks for joining me for this little sidebar. I hope you will review your sidebar logic and spend some time organizing the when as well as the where to put content there.
What I have shared with you here are what I consider the main conditional tags to give you the general idea of the developers “time/space continuum.” These will get you started, but there are many more options and further reading on the WordPress.org Codex website. As always, any feedback on this tutorial would be appreciated.
(Editor’s Note: The same logic and techniques can be used elsewhere in theme, not just the sidebar. It’s a cool and useful trick when developing or tweaking themes.)
great tutorial..
thanks for this valuable information..
its gonna help me a lot in customizing my wp blog..
thanks a lot once again..
Hi inder! Thank you for the kind comment. Im really glad you found it useful. I’m currently working on another one for this website that I think you will find even more useful when it comes to adding dimesion to your theme design. Look for that in the next week okay?
Warm regards,
IdaWebCo – AKA Richard Paul
thank you for this code