Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 2 • The WordPress Syntax 37


output format of the date (the first string in the parameter), what goes before the outputted
date (the second string), and what comes after it (the third string). The fourth instruction you
can pass is a Boolean that tells the system whether to output (true by default). Say you want
to output a year-month-day date (Y-m-d according to the PHP manual for date functions;
WordPress can take them all) within a paragraph. The code would look like this:


<?php the_date( 'Y-m-d', '

', '

' ); ?>


However, if you want to use this with PHP outside of the WordPress functions for some
reason, outputting nothing, you can set it to false with the echo option that this template
tag has. This goes last and is a Boolean value; hence, you won’t put it within quotation marks:


<?php the_date( 'Y-m-d', '

', '

', FALSE ); ?>


This would give you the same result, being year-month-day within a

tag, but it would
output nothing, so if you want to use it with your own PHP code, this is the way to go about
it. In other words, this is for assigning the_date() to a variable and not for direct output.


Remember that strings are text within quotation marks, integers are whole numbers, and
Boolean parameters are true or false without any quotation marks. With this in mind, it’ll
be a lot easier to understand the template tags you’ll use to build really cool WordPress sites
later on.


Don’t hardcode the date format in the_date() or the_time() unless you know
what you’re doing. These are settings that are available in WordPress, so most of the
time you don’t need to worry about them at all.

CONDITIONAL TAGS


Conditional tags are very handy. You use them in your theme’s template files, and as the name
implies, they are for setting various conditions. In other words, you can use them to display
different things depending on the situation. A very good example is the conditional tag
is_front_page(), which checks whether the page you’re on is the front page. Use it to
output a greeting because that is the polite thing to do:


<?php
if ( is_front_page() ) {
echo '

Hey you, welcome to the site. I love new
visitors!

';
}
?>


This would output a paragraph with the class welcome and the text within. You have a simple
test to determine if the page is_front_page() and then an echo with the paragraph if it
is in fact the home page. That’s very straightforward, so you can go on to try something else.
Suppose you’ve got a specific category that should have a different sidebar than the rest of the

Free download pdf