Languages: English • 日本語 Русский • Português do Brasil • ไทย • (Add your language)
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.
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.
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.
<?php bloginfo('name'); ?>
<?php bloginfo('description'); ?>
<?php bloginfo('url'); ?>
<?php bloginfo('admin_email'); ?>
<?php bloginfo('version'); ?>
To show the WordPress version, the template tag would look like this:
<p>Powered by WordPress version <?php bloginfo('version'); ?></p>
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.
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:
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('¥ '); ?>
Which, when the page is generated, would look like this:
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('¥ ', ' »'); ?>
Notice, we added another space before the arrow to separate it from the post title when the page is generated for viewing.
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.
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.
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:
An example of this category list might be:
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:
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:
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'); ?>
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.
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.