Template Tags, Values, and Parameters for Your WordPress Site - dummies

Template Tags, Values, and Parameters for Your WordPress Site

By Lisa Sabin-Wilson

Here, you discover the concept of parameters and values and how to use them, as well as the different types of parameters in WordPress. You should have a primary grasp of these concepts before moving forward with using template tags within your WordPress theme files.

If every piece of content on your site were hard-coded, it wouldn’t be easy to use and modify. Template tags allow you to add information and content dynamically to your site. One example of adding information using a template tag is the the_category tag. Rather than typing all the categories and links that each post belongs in, you can use the the_category() tag in your template to automatically display all the categories as links.

When you use a template tag, you’re really telling WordPress to do something or retrieve some information. Often, template tags are used to fetch data from the MySQL database and display it on your website. More than 100 template tags are built in to WordPress, and the tags vary greatly in what they can accomplish.

Template tags can be used only inside PHP blocks. The PHP blocks can be opened and closed as many times as needed in a template file. Once opened, the server knows that anything contained in the block is to be translated as PHP.

The opening tag (<?php) must be followed, at some point, by the closing tag (?>). All blocks must contain these tags. A template tag is used in the same way that PHP functions are. The tag is always text with no spaces (may be separated by underscores or dashes), with opening and closing brackets, and with a semicolon. The following line of code shows you how it all looks:

<?php template_tag_name(); ?>

Because a template tag is a PHP function, you can pass parameters to the tag. A parameter is simply a variable that allows you to change or filter the output of a template tag. Here are the three types of template tags in WordPress:

  • Tags without parameters: Some template tags don’t require any options, so they don’t need any parameters passed to them. For example, the is_user_logged_in() tag doesn’t accept any parameters because it returns only true or false.

  • Tags with PHP function–style parameters: Template tags with PHP function–style parameters accept parameters that are passed to them by placing one or more values inside the function’s parentheses. For example, if you’re using the bloginfo(); tag, you can filter the output to just the description by using

    <?php bloginfo(‘description’); ?>

    If there are multiple parameters, the order you list them is very important. Each function sets the necessary order of its variables, so double-check the order of your parameters.

    Always place the value in single quotations and separate multiple parameters by commas.

  • Tags with query string–style parameters: Template tags with query string–style parameters allow you to change the values of just the parameters you require. This is useful for template tags that have a large number of options. For example, the wp_list_pages() tag has 18 parameters. Rather than using the PHP function–style parameters, this function allows you to get to the source of what you need and give it a value. For example, if you want to list all your WordPress pages except for page 24, you’d use

    <?php wp_list_pages(‘exclude=24’); ?>

    Query string–style parameters can be the most difficult to work with because they generally deal with the template tags that have the most possible parameters.

This chart helps you understand the three variations of parameters used by WordPress.

Variation Description Example
Tags without parameters These tags have no additional options available. Tags without
parameters have nothing within the parentheses.
Tags with PHP function–style parameters These tags have a comma-separated list of values placed within
the tag parentheses.
Tags with query-string parameters These types of tags generally have several available
parameters. This tag style enables you to change the value for each
parameter without being required to provide values for all
available parameters for the tag.
Also accepts multiple parameters:

The WordPress Codex page has every conceivable template tag and possible parameter known to the WordPress software.