The Tuts+ Guide to Template Tags: Introduction

We've stated many times on this website that WordPress is a very powerful content management system and that its power comes from its extensibility. If a product is extensible, people will contribute to it, and if people contribute to it, the product grows and it will be discovered by more people, who will contribute, and they will help the product grow even more... It's this great cycle that builds communities, and this cycle is the reason for WordPress being the most popular content management system in the world.

In this series of articles, you're going to learn about one of the fundamental concepts of WordPress: template tags.

What Are Template Tags?

The two main features that demonstrate the extensibility of WordPress are being able to develop "plugins" (to increase functionality) and "themes" (to enhance design). These features are the result of the success of WordPress's core concepts like APIs and sub-systems. One of these concepts is the one you've started reading about—template tags.

Template tags are a core concept of WordPress that dates back even before WordPress: b2, the "weblog tool" that Matt Mullenweg and Mike Little forked to create WordPress, had template tags. You might say that it's one of the oldest features of WordPress that came before the ability to create themes and plugins. Show some respect for the elderly, people.

In the Codex, template tags are explained as follows:

Template tags are used within your blog's Templates to display information dynamically or otherwise customize your blog, providing the tools to make it as individual and interesting as you are.

In short, template tags are PHP functions that make WordPress do stuff for you. And trust me, it's not going to get any more complicated or confusing for you, if you know the basics of PHP and HTML.

Where Are These Template Tags?

You can find the files that store the functions for all template tags in the wp-includes folder. There are nine different files with the -template suffix:

  • wp-includes/author-template.php for author-related template tags
  • wp-includes/bookmark-template.php for bookmark-related template tags
  • wp-includes/category-template.php for template tags about all taxonomies and terms, including categories and tags
  • wp-includes/comment-template.php for template tags of the comments section
  • wp-includes/link-template.php for template tags about links (permalinks, attachment links, archive links etc.)
  • wp-includes/nav-menu-template.php for template tags of navigation menus
  • wp-includes/post-template.php for post-related template tags
  • wp-includes/post-thumbnail-template.php for template tags about post thumbnails
  • wp-includes/general-template.php for other template tags that can be used anywhere

Using Template Tags in WordPress

As I said before, the concept of "template tags" is not at all complicated if you know your way into the basics of PHP, HTML, and maybe CSS (mostly PHP). Since template tags are pretty much nothing but PHP functions, using them should be straightforward. However, there are a few things to know about them.

Template Tag Functions

Apart from "Include Tags" and "Conditional Tags" (which are a part of the "Template Tags" family), template tag functions can be divided into two groups: the ones that echo stuff and the ones that return stuff. These two types of functions form the base of template tags and allow you to develop new themes.

You can distinguish the returners by their names—they have the get_ prefix before the function name. While they look like functions derived from the original function, they contain the original code and return the output. The functions that echo stuff are usually "aliases" that echo the get_ functions. Here's the original source code of the the_ID() function:

Of course, there are functions for which you can pass a boolean $echo parameter—if you set it to TRUE, it echoes the output and returns if you set it to FALSE. Don't worry if this confuses you—we'll get to look at all parameters of all template tags.

Parameters of Template Tag Functions

This is the "hard part" of template tags, but it's very easy nonetheless.

Parameters are types of data that can change the behavior of template tag functions. With parameters, you can add prefixes and suffixes to the output, limit the number of returned data, exclude items from a list, and so on. Without knowing the accepted parameters for a given template tag, you're limited to the default behavior.

Declaring Parameters

There are three ways to declare parameters for a template tag:

Using regular parameters: The majority of template tags accept regular, PHP-style parameters that you can separate with commas:

Using query-strings: Some functions accept query-string style parameters, like below:

But query-strings are a bit hard to read, and you actually need to learn how to write in query-string style; so I recommend using an array instead of query-strings.

Using arrays: A better, cleaner way to declare parameters is using an array instead of query-strings:

Remember that you can use arrays instead of query-string parameters and vice versa, but you can't use regular parameters instead of query-string parameters or use an array instead of regular parameters. If a template tag accepts regular parameters, you can only use regular parameters. If it accepts query-string parameters or an array, you can use either.

Be sure to check out a whole page dedicated to the parameters of template tag functions, if you're confused.

Conclusion

Now that we've completed the basics of using template tags in our WordPress themes, we can move on to learning each one. In the next parts of this series, we're going to go through every template tag (documented in the Codex) with small descriptions, explanations of parameters, and little pieces of code on how to use each tag. It's going to be a long but great journey.

Do you have anything to share with us about template tags? Tell us what you think by commenting below—and if you liked this article, don't forget to share it with your friends!

Tags:

Comments

Related Articles