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

(avery) #1

36 PART I • Getting Started with WordPress


Simple enough to read and work with, right? Now compare that to the query-string style:

<?php
wp_tag_cloud( 'smallest=10&largest=24unit=px&orderby=count&order=RAND' );
?>

This is less than user friendly. The lesson is that there is such a thing as query string, but if
you’re passing multiple arguments to a parameter, then use the function style. It will make
things easier on you. And, as noted previously, this is currently the preferred method.

To recap this code: The order value, RAND, is in capitals. That is intentional; it is just how you
pass data to order (the other options are ASC for ascending and DESC for descending). Also,
you probably noticed that both smallest and largest were placed before the unit
option. It is good form to put the various parameters in the order they are described, as you’ll
be able to find them more easily whenever you need to edit the code or look something up.

UNDERSTANDING DATA TYPES
There are three types of data that you can pass to template tags: strings, integers, and
Booleans. Although the template tag’s definition (as stated in the WordPress Codex wiki) will
tell you exactly how to pass data to that particular template tag, it’s important to know what’s
behind it so that you can select the correct type.

The first data type is strings, which are lines of text. The bloginfo( 'name' ) example is
a string because you tell it that 'name' is the parameter. Strings are found within single or
double quotation marks (they do the same thing), although the single version is a lot more
common and the one used in the examples in this book.

The second data type is integers. Integers are whole numbers, such as 55900 or -3. You can
pass them inside quotation marks if you want, but you don’t need to. They are commonly used
when you need to fetch something that has an ID, which is a lot of things. You’ll stumble onto
template tags as well as conditional tags that do this later on.

Finally, there are the Boolean parameters, which are used when the value can be only true or
false. You can pass this information with all capitals (TRUE or FALSE), all lowercase letters
(true or false), or using numbers ( 1 being true and 0 being false).You cannot put Boolean
values within quotation marks; they always stand on their own. For example, the get_
calendar() template tag takes only one instruction, and that is whether to display the full
day or just a one-letter abbreviation. true is the default value and displays the first letter in
the name of the day (for example, M for “Monday”), so if you want to output Mon instead of
M, you need to set get_calendar() to false:

<?php get_calendar( FALSE ); ?>

Another example of Boolean instructions is the the_date() template tag, usually used to
output the date of a post. You may, for example, want to use that information in your own
PHP script instead and display nothing from the the_date() tag. You can change the
Free download pdf