Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Stepping Into Template Tags

If you take a peek into the header.php template file that came with your WordPress Theme, you will notice that where it says "My Blog Name", whatever it is, when you view your WordPress site, it doesn't say "My Blog Name" in the template file. In fact, it has a bunch of strange arrows and parentheses and words that don't make much sense.

This is an example of a Template Tag.

Let's take a few steps toward learning more about what these are and how they work.

What is a Template Tag

A template tag is code that instructs WordPress to "do" or "get" something. In the case of the header.php template tag for your WordPress site's name, it looks like this:

<h1><?php bloginfo('name'); ?></h1>

The template tag is <?php bloginfo(); ?> wrapped in an H1 heading tag. The bloginfo() tag gets information from your User Profile and Options > General in the Administration Panels. In the example here, the word name inside of the quote marks in the tag instructs the tag to "get the blog's site name". This is called a parameter.

Template Tag Parameters

In addition to the name parameter in the <?php bloginfo(); ?> template tag, there is other information that can be displayed. Let's look at a few of these parameters - and you can find more information and examples on the bloginfo() Codex page.

name <?php bloginfo('name'); ?>
As mentioned, this displays the name of the site and is set by the administrator in the Options > General SubPanel by default.
description <?php bloginfo('description'); ?>
This is called the "Tagline" for your blog which is usually some kind of descriptive sentence that says "My blog is about....". It is set by the administrator in the Options > General SubPanel.
url <?php bloginfo('url'); ?>
When you want to display the URL or website address for your WordPress site, you can use URL and it will show up. This also comes from the Options > General SubPanel.
admin_email <?php bloginfo('admin_email'); ?>
If you want to display the email of the administrator, you don't have to type it into the template files. By doing so, it may be open to email harvesters who use sophisticated software to come in and grab email addresses to use for spam. By using bloginfo('admin_email'), the email is displayed on the page for the viewers, but the actual email address is disguised from the harvesters. Nice, huh? The administrator's email address is set in the Options > General SubPanel.
version <?php bloginfo('version'); ?>
Sometimes you'd like to show off which version of WordPress you are using. The Themes that come with WordPress by default include this information in the footer template. It simply displays the version of WordPress your blog uses.

To show the WordPress version, the template tag would look like this:

<p>Powered by WordPress version <?php bloginfo('version'); ?></p>
Powered By WordPress version 6.7

Notice that only the version number is generated by the version parameter, not the words "Powered by WordPress version". Those were written in before the tag so they would be visible on the web page.

To learn more about template tag parameters, see Anatomy of a Template Tag and How to Pass Tag Parameters.

How Do You Use Template Tags?

Going through the various template tags in the Template Tags menu on the Codex, you will see that many of them are very simple, like the bloginfo() template tag, but many look very complicated to use. Let's look at some examples of how they are used to help you understand the "language" of the template tag codes.

As we saw in the bloginfo() template tag, all it took was one word to change the output of the tag. This word is called a parameter and it instructs the template tag to do or get something. In this case, the instruction is to get name which displays the site's name.

The template tag the_title() displays the title of the post, usually at the top of your post article. This tag gets the post title and displays it, by default, but it also has a do with the parameters which will help you change the look and presentation of the post title.

By default, the tag looks like this:

<?php the_title(); ?>

And the results look something like this:

Using WordPress Makes Me Smile

Let's say you want to put some kind of reference that highlights the title in some way, like a graphic or character entity like an arrow or bullet. Let's put a yen sign, ¥ ,the sign for Japanese money, in front of our title.

If you look carefully at the instructions for the tag the_title(), you will see that the parameters are:

<?php the_title('before', 'after', display); ?> 

We want the yen sign to be before the title, with a space after the yen sign and before the title, so let's add it to the parameters:

<?php the_title('&yen; '); ?> 

Which, when the page is generated, would look like this:

¥ Using WordPress Makes Me Smile

Now, let's take this a little further and put something after the post title. Let's say you want to encourage people to read so we'll add a little incentive arrow ( » ) to motivate them.

<?php the_title('&yen; ', ' &raquo;'); ?> 

Notice, we added another space before the arrow to separate it from the post title when the page is generated for viewing.

¥ Using WordPress Makes Me Smile »

You can also style your title heading in many different ways. Here is another example using heading tags.

<h2><?php the_title('Post Title: '); ?></h2> 

We've put the entire post title into an H2 heading and added the phrase "Post Title" to the beginning of the post title.

Post Title: Using WordPress Makes Me Smile

Note: Not all template tags take before and after arguments, though the_title does. Check the codex page for the specific tag you're using to see what arguments it accepts.

Boolean Template Tags

The above template tag example uses simple parameters separated from each other with quote marks and commas. Now consider examples of Boolean Template Tags that connect more than one parameter together using boolean math techniques. One common boolean expression uses the "and (&)" logic to connect the parameters.

The template tag wp_list_cats() is commonly found in the WordPress sidebar or menu template file. It lists the site's Categories.

<?php wp_list_cats(); ?>

By default, some of the template tags' parameters are:

  • all - Displays all of the Categories
  • sort_column - Sorts by Category ID
  • sort_order - Sorts in ascending order
  • list - Sets the Categories in an unordered list (<ul><li>)
  • optioncount - Does not display the count of posts within each Category
  • hide_empty - Based upon the first two parameters (optionall and all), does not display Categories without posts
  • use_desc_for_title - Uses the Category description as the link title
  • children - Shows the children (sub-Categories) of every Category listed

An example of this category list might be:

  • Stories About My Life
  • Stories About My Family
  • Things I Want To Share
    • About WordPress
    • About Writing
    • About Story Telling
  • Facts and Fiction About Life

The indented list with "About WordPress", "About Writing", and "About Story Telling" are the children or sub-Categories of the parent Category "Things I Want To Share". These titles, by default, are not the actual titles of the Categories, they are the descriptions of the Category you set in the Administration > Manage > Categories panel.

If you would like to show the actual title of the Category, instead of the Category description, change the template tag to:

<?php wp_list_cats('use_desc_for_title=0'); ?>

The zero sets the parameter to false, turning off the use of the description as the title. Now the Category titles would appear:

  • My Life Stories
  • My Family
  • Sharing
    • WordPress
    • Writing
    • Story Telling
  • Facts and Fiction

Let's say that you don't want the sub-Categories for "Sharing" to appear on your list. You would then add the parameter to not show the children, along with the parameter for showing only titles and not descriptions, with the boolean "and" using the ampersand ( & ).

<?php wp_list_cats('use_desc_for_title=0&children=0'); ?>

Notice there are no spaces around the ampersand. All the parameters run together without any spaces or quote marks in between, just around the whole parameter. Now the Category titles would appear as:

  • My Life Stories
  • My Family
  • Sharing
  • Facts and Fiction

As another example, if you want to display the Category links as the Category title, sort the list alphabetically by name, show the number of posts within each Category, and only show the children (sub-Categories) of Category ID number 3 ("Sharing"), the template tag would look like this:

<?php wp_list_cats('sort_column=name&sort_order=asc&optioncount=1&use_desc_for_title=0&child_of=3'); ?>
    • Story Telling (21)
    • WordPress (23)
    • Writing (10)

Template Tags and The Loop

Many of WordPress' template tags work within the WordPress Loop. This means that they are included in the template files as part of the php "loop" that generates the pages the viewer sees based upon the instructions inside of the Loop.

The WordPress Loop begins with:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Template tags that work within the loop must be in the middle area here, before the ending section of the Loop below:

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Template tags that need to be inside of the loop include the_content(), the_excerpt(), next_post(), and previous_post(). If the template tag you want to use doesn't have to be within the Loop, like wp_list_cats() and wp_list_pages(), then you can put it anywhere you like, for instance in the sidebar, header, or footer template files.

Learning More About Template Tags

This is just a tiny step into learning about the various powerful template tags WordPress uses to generate your website. You can learn more about the different template tags WordPress uses in the following articles and resources.

Styling Your Template Tags


External Resources