Exploring Template Tags, Values, and Parameters

Some people are intimidated when they look at template tags. Really, they're just a simple bit of PHP code that you can use inside a template file to display information dynamically. Before starting to play around with template tags in your WordPress templates, 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 to pull information from the MySQL database. Every tag begins with the 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 WordPress to grab the data and display it.

A typical template tag looks like this:

<?php get_info(); ?>

This entire 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 this case, get_info is the actual tag function, which grabs information from the database to deliver it to your blog. What information is retrieved depends on what tag function appears between the two PHP commands. As you may notice, there's a lot of starting and stopping of PHP throughout the WordPress templates. The process seems as though it would be resource intensive, if not exhaustive — but it really isn't.

image For every PHP command you start, you need a stop command. Every time a command begins with <?php, somewhere later in the code is the closing ?> command. PHP commands that aren't structured properly cause really ugly errors on your site, and they've been known to send programmers, developers, and hosting providers into loud screaming fits.

Understanding the basics

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 by using a template tag is the the_category tag. Instead of 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.

Using template tags prevents duplication of effort by automating the process of adding content to your Web site.

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 server and even display it on the front end. More than 100 template tags are built into WordPress, and the tags vary greatly in what they can accomplish. A complete list of template tags can be found in the WordPress Codex at http://codex.wordpress.org/Template_Tags.

Template tags can be used only inside of PHP blocks. The PHP blocks can be opened and closed as many times as needed in a template file. When 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), opening and closing brackets, and a semicolon. The following line of code shows you how it all looks:

<?php template_tag_name(); ?>

PHP is a fairly advanced coding language, and has many built-in functions for you to use. If you are not a PHP developer, I recommend that you keep it simple when you're attempting to add custom PHP. All code must be semantically perfect or it will not work. Always read your code to make sure that you entered it correctly.

image Some template tags can be used only inside the loop so check the Codex for details. You can find out more about the loop in the section titled “Examining 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 simply a variable that allows you to change or filter the output of a template tag. There are 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 only returns 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'), ?>

    image If there are multiple parameters, the order in which 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 quotes, 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. Instead of 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 use
    <?php wp_list_pages('exclude=24'), ?>

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

Table 3-1 helps you understand the three variations of parameters used by WordPress.

Table 3-1 Three Variations of Template Parameters

image

image The WordPress Codex, located at http://codex.wordpress.org, has every conceivable template tag and possible parameter known to the WordPress software. The tags and parameters that I share with you in this chapter are the ones used most often.

Customizing common tags

Because template tags must be used inside the PHP template files, they can easily be customized with HTML. If you're using the PHP tag wp_list_pages(), for example, you could display it in an HTML unordered list so that the pages are easily accessible to the users, like this:

<ul>
<?php wp_list_pages(); ?>
</ul>

This displays all the pages that you created in WordPress as an unordered list. If you had the pages About, Blog, and Content, it would be displayed like this:

  • About
  • Blog
  • Contact

Another example is titles. For proper search engine optimization, you should always put page titles in H1 HTML tags, like this:

<h1 class="pagetitle">
<?php the_title(); ?>
</h1>

Digging deeper into the WordPress Codex

One of the best resources you can use for expanding your knowledge of WordPress is the WordPress Codex (http://codex.wordpress.org) shown in Figure 3-4. The Codex offers details, information, and examples for all theme templates, template tags, and theme creation in general. We use it when working on a theme because it has extensive and up-to-date information about functions and template tags.

Figure 3-4: The WordPress Codex.

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.46