Template tags are functions defined in the core WordPress files and are globally available for use in your templates. There are a lot of tags available for different aspects of your template files. To get a complete list of what’s available see the Template Tags on the WordPress Codex.
Some accept many parameters (information passed to the function enclosed in the brackets/parentheses), others do not accept any parameters. To use a template tag you need to ensure it’s within opening and closing PHP tags e.g.
<?php template_tag_name('parameter_name=value¶meter2=value'); ?>
If the tag is going into an already defined set of PHP code then the opening and closing PHP tags are not needed. For those that do not require or accept parameters then you can omit the content within the brackets/parentheses i.e.
<?php template_tag_name(); ?>
Understanding the tags and parameters, and how they can work for you means you can gain control of your own theme, without having to resort to hardcoding your pages, categories or anything else. Often what you want to do can usually be accomplished with a combination of template tags along with conditional tags.
I plan to cover the more popular / important tags used among bloggers, which should give you more of an understanding of what’s going on in your existing theme and how you can change a few parameters to get your desired output.
The tag for the category listing is wp_list_categories(). Without any parameters, this will create the following:
<li class="categories">Categories
<ul>
<li class="cat-item cat-item-1">
<a href="http://www.yourdomain.com/category/category-name/" title="Category Description here if set">Category Name</a>
</li>
</ul>
</li>
While the default listing is fine, you may want to gain a little control over what is output. The more popular parameters are:
So, if you wanted to display the post count after each category, exclude a particular category (ID of 4) and remove the title and list tags, as you going to style the list in a definition list, you’d use:
<dl>
<dt>Categories</dt>
<dd><?php wp_list_catgories('show_count=1&exclude=4&title_li='); ?></dd>
</dl>
Which would result in output as
<dl>
<dt>Categories</dt>
<dd><ul>
<li class="cat-item cat-item-1">
<a href="http://www.yourdomain.com/category/category-name/" title="Category Description here if set">Category Name</a>
</li>
</ul></dd>
</dl>
You may wish to show a tag cloud instead of your category list (or as well as). A lot of the older themes out there will not contain this function as it’s only been available since WordPress 2.3, however it’s relatively simple to add into your sidebar.
The template tag to get a tag cloud is wp_tag_cloud().
The default of this will print out a string of your tags alphabetically, in varying sizes (controlled by the pt unit), separated by a space (so no markup involved). There are a number of parameters you can set to control how your tags are displayed.
So, if you wanted to insert your tag cloud in your sidebar, display up to 30 tags, change the text size to use px, adjust the font size and display the tags randomly, then you would use:
<p><?php wp_tag_cloud('smallest=7&largest=14&unit=px&number=30&order=RAND'); ?></p>
4 Responses to “Template Tags, Categories and Tag Clouds”
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed.
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums