• Save
Take Control of Your Templates with WordPress Conditionals
Upcoming SlideShare
Loading in...5
×
 

Take Control of Your Templates with WordPress Conditionals

on

  • 7,148 views

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 ...

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!

Statistics

Views

Total Views
7,148
Views on SlideShare
7,128
Embed Views
20

Actions

Likes
4
Downloads
0
Comments
2

4 Embeds 20

http://paper.li 12
http://lanyrd.com 6
https://twitter.com 1
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • A recording of this presentation has been posted on WordPress.tv:

    http://wordpress.tv/2011/10/08/kathryn-presner-taking-control-of-your-templates-with-wordpress-conditionals/
    Are you sure you want to
    Your message goes here
    Processing…
  • The code font formatting got mangled a bit during the Slideshare conversion - apologies.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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
  • Conditionals that take arrays are in the Codex

Take Control of Your Templates with WordPress Conditionals Take Control of Your Templates with WordPress Conditionals Presentation Transcript

  • Take Control of your Templates with WordPress Conditionals with Kathryn Presner WordCamp Montreal #wcmtl July 2011
  • Kathryn Presner of Zoonini Web Services www.zoonini.com @zoonini on Twitter about.me/kpresner
  • What’s coming up:
    • why conditional tags are powerful and useful
    • if/else statements – working some PHP magic (if you’re new to PHP, don’t be scared - we’ll start off slowly!)
    • WordPress conditional tags – an intro
    • let’s get our hands dirty – live examples
    • questions
  • The challenge... few theme files generating many pages & posts index.php comments.php footer.php functions.php header.php page.php sidebar.php single.php style.css mysite.com mysite.com/about/ mysite.com/contact/ mysite.com/services/ mysite.com/services/web-design/ mysite.com/services/web-design/wordpress/ mysite.com/services/programming/ mysite.com/services/graphic-design/ mysite.com/blog/ mysite.com/blog/2011/ mysite.com/blog/2011/july/ mysite.com/blog/2011/july/day1-wordcamp/ mysite.com/blog/2011/july/day2-wordcamp/ mysite.com/blog/category/wordpress/ mysite.com/blog/category/php-stuff/ mysite.com/blog/tag/orange/ mysite.com/blog/tag/red/ mysite.com/blog/tag/green/ mysite.com/blog/tag/yellow/ For example:
  • Why are conditionals useful... different sidebars elements on different pages
  • Why are conditionals useful... different sidebars elements on different pages
  • Why are conditionals useful... different sidebars elements on different pages
  • Why are conditionals useful... include an external file on a certain page
    • display a hippopotamus photo above all posts in the “Hippopotamus” category
    • show a strawberry shortcake recipe in the sidebar of all posts tagged with “strawberry”
    • display links to your Flickr and Twitter accounts in the footer of three specific pages
    More reasons conditionals are useful...
    • PHP if/else statements + WordPress conditional tags
    • = template awesomeness
    OK, I’m convinced. So how does it work?
    • DON’T PANIC!
    • A little bit of PHP goes a long way.
    • Let’s learn some right now...
    Uh, did you just say PHP?
    • If you are thirsty,
    • drink some water.
    Pseudo-code first (baby steps!)
    • <?php if ( is_thirsty() ): ?>
    • <p>Drink some water.</p>
    • <?php endif; ?>
    Now let’s PHP-ify it
    • <?php open the PHP code block
    • ?> close the PHP code block
    • Opening and closing PHP tags go before and after your PHP code.
    What do these lines mean?
    • if ( ):
    • STUFF HERE
    • endif;
    • The if line checks whether a certain condition is TRUE or FALSE. The condition must be (in brackets)
    • The stuff that goes between the if and endif bits is ONLY executed/displayed if the condition we are checking for is TRUE.
    What do these lines mean?
    • is_thirsty()
    • Made-up function that checks whether you are thirsty. A function is shortcut name for a chunk of code you can reuse in different places. It’s defined elsewhere.
    • <p>Drink some water.</p>
    • Regular HTML paragraph.
    What do these lines mean?
    • <?php if ( is_thirsty() ): ?>
    • <p>Drink some water.</p>
    • <?php endif; ?>
    • This code says: if you are thirsty, drink some water.
    • You will only drink some water if the condition is_thirsty() is TRUE.
    Back to our code...
    • Pseudo code: If you like strawberries OR if you are thirsty, drink a strawberry smoothie.
    • PHP:
    • <?php if ( is_thirsty() || is_strawberryfan() ): ?>
    • <img src=&quot;http://www.mysite.com/smoothie.gif&quot; height=&quot;50&quot; width=&quot;30&quot; />
    • <?php endif; ?>
    How about something a tad more complicated?
    • is_thirsty()
    • is_strawberryfan() ....are made-up functions
    • || ...is the OR operator that checks to see if the first condition OR the second condition is true
    • <img src=&quot;http://www.mysite.com/smoothie.gif&quot; height=&quot;50&quot; width:&quot;30&quot; /> ...is a picture of a giant smoothie.
    What does this mean?
    • <?php if ( is_thirsty() || is_strawberryfan() ): ?>
    • <img src=&quot;http://www.mysite.com/smoothie.gif&quot; height=&quot;50&quot; width=&quot;30&quot; />
    • <?php endif; ?>
    • This code says: if the person is thirsty OR the person is a strawberry fan, it’s smoothie time!
    • NOTE: If you want AND instead or OR, use &&
    Back to our code...
    • BEHOLD:
    •  
    • The Codex list of WordPress Conditional Tags - bookmark it, baby!
    •  
    • http://codex.wordpress.org/Conditional_Tags
    Where do I find the real functions?
    • Is it the homepage?
    • is_front_page()
    • Is it a single post?
    • is_single()
    • Is it a single post with the ID 234, 453 or 524?
    • is_single(array (234,453,524))
    • Is it a sticky post?
    • is_sticky()
    •  
    A few examples: WordPress conditionals are pre-defined functions that check whether a certain condition is TRUE. Is it a page with the slug “brownies“? is_page('brownies')  How about a post in the recipes category, which has the ID 12? in_category('12')  Is it the recipe category archives page? is_category('recipes')  Or a post that is tagged with &quot;chocolate”? has_tag('chocolate') 
    • In your your blog’s header, on category archive pages for the:
    • Halloween category, show a pumpkin
    • Valentine’s Day category, show a cupid
    • St. Patrick’s Day category, show a four-leaf clover
    • In any other category archive, show a photo of your cat.
    Let’s try another example:
    • In header.php where you want the image to appear, put:
    • <?php if ( is_category('halloween') ): ?>
    • <img src=&quot;http://www.mysite.com/pumpkin.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php elseif ( is_category('valentines') ): ?>
    • <img src=&quot;http://www.mysite.com/cupid.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php elseif ( is_category('stpatricks') ): ?>
    • <img src=&quot;http://www.mysite.com/clover.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php else: ?>
    • <img src=”http://www.mysite.com/cat.gif&quot; height=&quot;50&quot; width=”50&quot; />
    • <?php endif; ?>
    •  
    • elseif is checking whether another condition is TRUE
    • else is the default condition, which will be met if none of the above is TRUE
    • PHP can be interspersed with (X)HTML – just remember to put all the PHP bits in between opening and closing PHP tags. <?php your PHP code here ?>
    • Where do these PHP statements go? Your theme files. Could be footer.php, header.php, page.php.... depending where you want the end result to go.
    • Conditionals can save us from having to create and manage extra theme files, making your site more efficient and requiring less maintenance.
    Things to Remember:
  • A Note About Not
    • You can test a condition for its opposite by putting ! in front of it. For example, this conditional displays the image of the happy hippo only when the page is NOT the front page.
    • <?php if ( ! is_front_page() ): ?>
      • <img src=&quot;http://www.mysite.com/happy-hippo.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php endif; ?>
  • A Note About Arrays
    • To test for multiple instances of the same condition with certain conditionals, use an array. In this example, if the page’s slug is bio, about, or contact, the happy hippo image will be displayed.
    • <?php if ( is_page ( array(’bio’,'about’, 'contact' ) )   ): ?>
    • <img src=&quot;http://www.mysite.com/happy-hippo.gif&quot; height=&quot;50&quot; width=&quot;50&quot; /> <?php endif; ?>
  • A Note About if/else Syntax
    • The PHP if/else syntax used in this demo is an alternative form which can be easier to read when mixed with HTML, as in WordPress templates. “Classic” PHP if/else syntax uses curly brackets (aka “braces”). Example:
    • <?php if ( is_front_page() ) { ?>
    • <img src=&quot;images/logo.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php } else { ?>
    • <img src=&quot;images/cat.gif&quot; height=&quot;50&quot; width=&quot;50&quot; />
    • <?php } ?>
  • Resources
    • Codex entry on Conditional Tags http://codex.wordpress.org/Conditional_Tags
    • Widget Logic plug-in http://wordpress.org/extend/plugins/widget-logic/
    • PHP if/else syntax http://php.net/manual/en/control-structures.alternative-syntax.php http://php.net/manual/en/control-structures.if.php http://php.net/manual/en/control-structures.elseif.php http://www.w3schools.com/php/php_if_else.asp
  • LET’S GO! Time for the live demo