Ever wonder how to put a giant porcupine photo at the top of your homepage - but a happy hippo in the header everywhere else? Want to know how to display a list of favourite sites on your blog - but on static pages have a sidebar with Twitter and Facebook links instead? Need to put that brownie recipe at the bottom of every page except the FAQ? Using WordPress conditional tags you can easily do all that and tons more. Whether you’re just starting to learn how themes work or you already know the basics and are ready to take your themes to the next level, this session is for you. Understanding conditionals is a key building block in crafting WordPress sites – a powerful skill that will let you do all kinds of nifty things with your templates. Once you see how handy WordPress conditional tags are, you’ll wonder how you ever managed without them!
Professional web designer, building sites since 1998, fell in love with WordPress 10 years later in 2008. Not a hard-core programmer, communications background. I’m a moderator in WP support forums, love helping people with questions that are solved by conditionals – people are always delighted when they see what’s possible, and I hope you will be too.
WordPress conditionals are an easy-to-use tool to help make your site more dynamic and efficient
WordPress generates multiple web pages from a small set of theme files, and the same code. For example, page.php generates all the static pages in your site, the header.php file includes the same code at the top of every page, same for footer.php and so on. But what if you want the footers to be different on some pages? Or different code to be included in your header.php on specific pages? You can create multiple theme files, but that can get “high maintenance” – more files to keep track of Conditionals to the rescue!
On one of my client’s site for an author and her book, on the homepage we have an image of the book cover in the sidebar
...but on other pages, we have a banner linking to her blog...
...and on the About the Author page, we have the banner PLUS a headshot of the writer. This is accomplished through one sidebar file... and WordPress conditional tags.
For this site, I wanted to incorporate my favourite form script, called Form-to-Email Pro. But I like to code forms in a certain way, and I couldn’t just paste it directly into the WordPress editor, as some of my tags would be stripped out. So instead, I put the form into a separate theme file, and edited the page.php file to use a conditional tag which includes the form code ONLY on the contact page.
Conditional tags let you make exceptions to the rules set out in your theme files, by instructing your theme files to do specific things under certain conditions.
PHP is a powerful scripting language, and conditional tags are special bits of code provided by WordPress that let you test for certain conditions. When combined in your theme files, they make magic.
I’m going to teach you a bit of basic right now – you won’t need to know much to be able to use WordPress conditionals.
Well, remember all those fake functions? Well we don't have to fake it anymore! This page is chock-full of goodness. The Codex is the official WordPress documentation and this page is invaluable. Not only does it list all the functions, it also gives practical examples.
There are conditional tags to check whether a page or post meets a zillion different conditions.
Here, we are checking three times to see whether two conditions are met – whether it’s a particular holiday and whether the front page of the blog is being displayed. If we wanted the photo to go in the sidebar, this code would go in the sidebar.php file