Chapter 2
35
Some of the more important ones are as follows:
Parameter Name Effect
Number
Pass a numeric value to limit the number of categories retrieved. This
is especially helpful for blogs with a large number of categories.
Feed
Pass true to cause a link to each category's feed to be printed
current_
category
Pass the ID of a category to force the output to contain the
current-cat class on a particular category.
For example, say you wanted to limit your category list to the rst ve categories. To do so, you
would use the following code:
<?php
wp_list_categories(array('number' => 5));
?>
For more information on the available parameters,
visit />Listing all of the tags in use on a blog
Generally, tags are used liberally to indicate the subject matter of a post. For this reason, a list
of tags is a great way to help visitors to get around a blog and view a wide array of posts that
they're interested in. By default, WordPress lists tags in a cloud, varying the size of each tag
according to the number of times it was used. However, this default output can be modied
to produce a list that might make more sense to your users.
How to do it
First, decide where you want to generate a linked list of all of your tags. Open the appropriate
template le, and insert the following:
<?php
wp_tag_cloud(array(
'format' => 'list',
<li>
<a style="" title="1 topic" class="tag-link-56"
href="al/tag/crushing/">crushing</a>
</li>
<li>
<a style="" title="1 topic" class="tag-link-58"
href="al/tag/dinarchy/">dinarchy</a>
</li>
<li>
<a style="" title="1 topic" class="tag-link-59"
href="al/tag/doolie/">doolie</a>
</li>
</ul>
You'll notice that each of the <a> tags that link to the tag archive page has an empty in-line
style attribute. This is a consequence of the processing that wp_tag_cloud does internally.
You'll also notice that unlike wp_list_pages and wp_list_categories, this function
produces a surrounding <ul> element for its items.
There's more
In most cases, the default display of wp_tag_cloud will not be the one that is most
benecial to your users. This is especially true for business blogs and professional
sites. Luckily, there is an easy way to change how wp_tag_cloud displays.
Passing parameters
As seen earlier, the output from wp_tag_cloud can be modied by using parameters with
the function call. Parameters are passed in the same way as with many other WordPress
functions, which is in the following format:
<?php
wp_tag_cloud(array('parameter_name' => 'parameter_value'));
?>
You've already seen a couple of the parameters that wp_tag_cloud supports. Another
important one is the number parameter, which limits the number of tags placed in the
style of the highlighted item to your heart's content.
Chapter 2
39
There's more
If your blog has a small number of top-level categories and you are using wp_list_
categories
for your main navigation items, you might want to take advantage of the
highlighting capabilities demonstrated for pages. Doing so is easy, because you can use
the earlier-featured code in full. You just have to change the targeted class from
current_page_item to current-cat, as follows:
.current-cat a {
color: #fff;
background: #000;
}
Adding a search function to a theme
In spite of your best efforts, static navigation for a website will always be left wanting when a
user wants to quickly and easily nd content matching a specic term or phrase. That is where
search comes in, and with WordPress, it is easy to implement.
How to do it
Open your theme and decide where you want to place the search form. The best place for a
search form is either in the header or at the top of a sidebar in the site. When you gure out
where you want to place the search form, insert the following code at the appropriate place:
<form method="get" id="searchform" action="<?php echo site_url('/');
?>">
<label class="hidden" for="s"><?php _e('Search for:'); ?></label>
<div>
<input type="text"
value="<?php echo attribute_escape(get_search_query()); ?>"
name="s" id="s" />
<input type="submit" id="searchsubmit"
the category doesn't exist.
How to do it
For this recipe, consider the situation where you need to link to three different categories:
Testimonials, Portfolio, and Thoughts. You've established each of these categories in your
local development environment and in your staging environment, but you haven't yet created
them on the blog where you'll be launching your theme. This is a good situation to use
conditional linking.
Chapter 2
41
Given this situation, you need code similar to the following:
<?php
$nav_categories = array('Testimonials','Portfolio','Thoughts');
?>
<ul id="site-nav">
<?php
foreach($nav_categories as $cat_name) {
$cat_id = get_cat_ID($cat_name);
if($cat_id) {
?>
<li>
<a href="<?php echo get_category_link($cat_id); ?>">
<?php echo $cat_name; ?>
</a>
</li>
<?php
}
}
?>
</ul>
This code produces a nice list of links for the categories that exist. You remove the chance
get_page_link respects the front page options of WordPress and bypasses a lot of checks
that get_permalink has for non-page links. If you know that you are linking to a page and
not a post, you should use get_page_link.
Creating a category drop-down menu
For highly-categorized and deeply-hierarchical sites, showing a full list of categories and
subcategories can take up a lot of space in your design. To get around this, you can change
your categories list from static to dynamic by using a simple JavaScript technique.
How to do it
First, download the Supersh package from />plugins/superfish/
and place all of the JavaScript les contained within it in your theme
directory. Next, insert the following code in your theme's <head> section, above the wp_head
function call:
<?php
wp_enqueue_script('superfish',
get_bloginfo( 'stylesheet_directory' ) . '/superfish.js',
array('jquery'));
?>
Chapter 2
43
Place the following code after the wp_head call:
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function() { jQuery('ul.superfish').
superfish(); });
// ]]>
</script>
Now, open the template le in which you wish to display your Category drop-down.
Insert the following:
<ul class="nav superfish">
<?php wp_list_categories(array('title_li'=>'','hide_empty'=>false));
hovering over a parent page:
And you should see the following after hovering over a parent page: