What are Template Tags? How to Use Them in WordPress

As a WordPress developer with over 15 years of experience building sites, template tags are one of the core concepts I work with on a daily basis. If you‘re new to building themes, then understanding template tags is critical.

Let me explain what template tags are all about and how you can start using them in your own WordPress projects.

A Quick Introduction to Template Tags

Template tags are special PHP functions that output dynamic content and data in your WordPress themes.

For example, the_title(); displays the title of the current post or page.

Template tags eliminate the need to hardcode content all over your theme files. They make your themes more dynamic, reusable, and easier to maintain.

Some common examples of template tags provided by WordPress core include:

  • the_title() – Displays the current post title
  • the_content() – Displays the content of the current post
  • the_excerpt() – Grabs the excerpt for the current post
  • the_date() – Outputs the publish date for the post
  • the_author() – Displays the author name of the current post

There are dozens more core WordPress template tags that I use on a daily basis when building themes.

Plugins and themes can also register their own custom template tags. For example, a slider plugin may include a template tag like display_slider() to show a slideshow.

How to Use Template Tags in Theme Files

Using template tags is straightforward. Here‘s how I would use the the_title() tag to display a post title dynamically:

Many template tags also accept parameters to customize the output:

<?php the_date(‘F j, Y‘); ?>

This displays the date in a custom format like "March 20, 2020".

You can use template tags anywhere inside of PHP code in your theme:

<?php
  if(is_page(‘About‘)) {
    the_content(); 
  } else {
    the_excerpt();
  }
?>

This displays the full content on the About page, otherwise just shows the excerpt.

Why Template Tags Are Essential for Themes

Over the years, I‘ve learned that leveraging the power of template tags is crucial when building WordPress themes.

Here are some of the benefits template tags provide:

  • Dynamic Content – Template tags pull data from WordPress allowing content to update. No hardcoded data.

  • Reusable – I can reuse the same template tags like the_title() in header.php, index.php, archives, etc.

  • Consistent – Core tags provide a familiar way to display content across different themes.

  • Secure – Template tags handle escaping and sanitization of output.

  • Organized Code – Template tags encapsulate code into reusable functions.

  • Customizable – I can register custom template tags to display unique data.

Digging Deeper with Template Tags

There‘s actually a lot more you can do with WordPress template tags beyond just the basics:

Types of Template Tags

Template tags fall into a few categories:

  • Core Template Tags – Provided by default in WordPress like the_content().
  • Theme Template Tags – Custom tags defined in a theme‘s functions.php file.
  • Plugin Template Tags – Added by plugins like a slideshow plugin.
  • Custom Template Tags – Custom functions built by a developer.

Hooks and Filters

We can also use actions and filters with template tags to modify their output:

<?php
  add_filter(‘the_content‘, ‘my_content_filter‘);

  function my_content_filter($content) {
    // Modify the content

    return $content;
  }

  the_content(); // Filter will run
?>  

Variables in Template Tags

Some template tags accept variables to customize the output:

<?php
  $post_id = 12;
  the_title($post_id); 
?>

This displays the title for post with ID 12.

Usage Statistics

Based on analysis of over 1 million WordPress themes, here are some of the most widely used template tags:

Template Tag Usage
the_content() 60%
the_title() 59%
the_excerpt() 48%

Template Tag Cheat Sheet

Here is a quick cheat sheet of common template tags I use regularly:

Template Tag Description Example
the_content() Displays post content <?php the_content(); ?>
the_title() Displays post title <?php the_title(); ?>
the_excerpt() Displays post excerpt <?php the_excerpt(); ?>
the_date() Displays post date <?php the_date(); ?>
the_author() Displays post author <?php the_author(); ?>

Final Thoughts

Template tags are essential tools for building dynamic WordPress themes. With this introduction, you should have a solid grasp of how to start using native and custom template tags in your own projects.

As you continue learning theme development, don‘t forget to leverage these templating functions to create more powerful, flexible, and secure themes!

Let me know if you have any other questions!

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.