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

(avery) #1

CHAPTER 2 • The WordPress Syntax 35


First, pass one argument to the parameter for wp_tag_cloud() using query-string style:


<?php wp_tag_cloud( 'unit=px' ); ?>


This is easy enough to understand; you’ve just set unit to px, an argument you learned about
in Table 2-1 (or through the Codex at http://codex.wordpress.org/Function_
Reference/wp_tag_cloud, which features the most up-to-date documentation). When
passing one, or even a few parameters, the query-string style makes sense at first glance. If you
want to pass more parameters, just add an ampersand (&) between them, within the
parameter, with no spaces. Change the tag order to count rather than name:


<?php wp_tag_cloud( 'unit=px&orderby=count' ); ?>


Simple enough, right? Now switch to the preferred function method instead and bear with me
on this because the reason will become apparent in a little bit. First, the preceding example,
using function style, would mean that you save the arguments in an array and then pass that
to wp_tag_cloud() instead, like this:


<?php
// The arguments
$args = array(
'unit' => 'px',
'orderby' => 'count'
);
// Pass the arguments
wp_tag_cloud( $args );
?>


The same arguments as before are stored in the array, which goes in $args, which in turn is
used as the parameter for wp_tag_cloud(). Simple as that.


You can add even more just by separating the various parameters for the template tag with
ampersands. Now randomize the order, changing the smallest tag to 10 px (because the
default 8 is a bit small when you’re using pixels rather than points) and the largest to 24 px.


Here’s the code in function style:


<?php
// The arguments
$args = array(
'smallest' => 10,
'largest' => 24,
'unit' => 'px',
'orderby' => 'count',
'order' => RAND
);
// Pass the arguments
wp_tag_cloud( $args );
?>

Free download pdf