Template Tags, Values, And Parameters In WordPress

 

The following is from WordPress All-In-One For Dummies, 2017, 3rd ed., pp. 449 – 451

Really, template tags are just simple bits of PHP code that you can use inside a template file to display information dynamically.

. . . it’s important to understand what makes up a template tag and why.

WordPress is based in PHP (a scripting language for creating web pages) and uses PHP commands topull information from MySQL database. Every tag begins with the function to start PHP and ends with a function to start PHP and ends with a function to stop PHP. In the middle of those two commands lives the request to the database that tells the WordPress to grab the data and display it.

A typical template tag looks like this:

<?php get_info ( ); ?>

This example tells WordPress to do three things:

  • Start PHP (<?php>.
  • Use PHP to get information from the MySQL database and deliver it to your blog (get_info ( );).
  • Stop PHP (?>).

In thiss case, get_info represents the tag function, which grabs information from your database to deliver it to your site. What information is retrieved depends on what tag function appears between the two PHP commands.

When you use a template tag, you’re really telling WordPress to do something or retrieve some information.

More than 100 template tags [really, tag functions] are built in to WordPress, and the tags [again, functions, I think] vary greatly in terms of what they can accomplish. You can find a complete list of template tags in the WordPress Codex (the documentation for WordPress) at https://wordpress.org/Template_Tags.

A template tag is used the same way that PHP functions are. The tag is always text with no spaces (may be seperated by underscores or dashes), opening and closing brackets, and a semicolon. This is what it looks like:

<?php template_tag_name ( ); ?>

Some template tags can be used only inside The Loop, so check the codex for details.

See upcoming post “The Main Index and The Loop.

Using Parameters

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

The above links are to the Codex and have excellent examples of the three types of Template parameters.

Here is Table 3-1  Three Variations of Template Parameters

Variation Description Example
Tags without parameters These tags have no additional options available. Tags without parameters have nothing within the parentheses. the_tag( );
Tags with PHP function-style parameters These tags have a comma-seperated list of values placed with the tag parentheses. the_tag( ‘1,2,3’ );
Tags with query string-style parapmeters 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. the_tag( ‘parameter=true’ );

 

 

This entry was posted in Coding, Wordpress. Bookmark the permalink.