Introduction

Template tags are the functions you sprinkle through theme files to print post titles, content, links, dates, menus, and site information. They are how a template turns query results into HTML without you writing raw SQL or touching the database.

This reference groups the template tags you reach for most, explains the echo-versus-return pattern that trips up beginners, and shows where each tag belongs. Keep it handy while building or editing a theme.

You will learn:

  • What a template tag is and how it reads post data
  • The difference between the_ and get_the_ versions
  • The most useful tags for content, links, dates, authors, images, and navigation
  • How to escape template tag output safely

This pairs with the Loop and template hierarchy articles. Most template tags only work inside the Loop, so understanding the Loop first makes everything here click.

What Is a Template Tag?

A template tag is a PHP function provided by WordPress that outputs or returns data about the current post, the site, or the current request. Many read from the global state that the Loop sets up, which is why they work without you passing a post ID.

Template tags fall into a few rough groups:

  • Post data tags – title, content, excerpt, permalink, ID.
  • Meta tags – author, date, categories, tags.
  • Media tags – featured images and attachments.
  • Site tags – site name, description, URLs.
  • Navigation tags – menus, pagination, next and previous links.

The Most Important Rule: the_ vs get_the_

Most template tags come in two forms, and choosing the right one prevents a whole class of bugs.

  • the_ versions echo their output directly to the page. Example: the_title() prints the title where you call it.
  • get_the_ versions return the value as a string instead of printing it. Example: get_the_title() hands you the title to store, test, or modify.

Use the get_ form whenever the value goes into a variable, a condition, or an HTML attribute. Use the echo form when you just want to print it in the markup.

<?php
// Echo form: print the title in an h2.
the_title( '<h2>', '</h2>' );

// Get form: use the title inside an attribute, escaped.
$title = get_the_title();
printf( '<img src="%s" alt="%s">',
    esc_url( get_the_post_thumbnail_url() ),
    esc_attr( $title )
);

Post Content Tags

  • the_title() / get_the_title() – The post title. The echo form accepts before and after markup arguments.
  • the_content() – The full post content, with filters applied (shortcodes, blocks, embeds). There is no direct get_ equivalent that applies filters; use apply_filters( "the_content", get_the_content() ) if you need the filtered string.
  • the_excerpt() / get_the_excerpt() – The post excerpt, either hand-written or auto-generated from the content.
  • the_ID() / get_the_ID() – The numeric ID of the current post.
  • the_permalink() / get_the_permalink() – The full URL to the post. get_permalink() is an equivalent older name.

Date and Time Tags

  • the_date() / get_the_date() – The post publication date. Note: the_date() prints only once per day in a loop of multiple posts on the same date; use get_the_date() inside markup to avoid that surprise.
  • the_time() / get_the_time() – The publication time, formatted by the argument you pass.
  • the_modified_date() / get_the_modified_date() – When the post was last updated.

All date tags accept a PHP date format string. Pass no argument to use the format set in the site settings.

<?php
// Always reliable inside a loop, with a custom format.
echo esc_html( get_the_date( 'F j, Y' ) );

Author Tags

  • the_author() / get_the_author() – The display name of the post author.
  • the_author_meta() / get_the_author_meta() – A specific field of the author, such as description or user_email.
  • get_the_author_posts_link() – A link to the author archive, returned as HTML.
  • get_author_posts_url() – Just the URL of the author archive.

Taxonomy Tags

  • the_category() / get_the_category() – the_category() prints a linked list of categories; get_the_category() returns an array of term objects to format yourself.
  • the_tags() / get_the_tags() – The post tags, as a printed list or an array of term objects.
  • get_the_term_list() – A linked list of terms from any taxonomy, including custom ones. Use this for custom taxonomies.
<?php
// Linked list of terms from a custom taxonomy.
echo wp_kses_post(
    get_the_term_list( get_the_ID(), 'genre', 'Genres: ', ', ' )
);
  • the_post_thumbnail() – Prints the featured image at a given size, for example the_post_thumbnail( "large" ).
  • get_the_post_thumbnail() – Returns the featured image HTML instead of printing it.
  • get_the_post_thumbnail_url() – Returns just the image URL, handy for background images or custom markup.
  • has_post_thumbnail() – Returns true if a featured image is set, so you can avoid empty markup.
<?php
if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'medium', array( 'loading' => 'lazy' ) );
}

Site Information Tags

These tags describe the whole site, not a single post, so they work anywhere.

  • bloginfo() – Prints a piece of site data. bloginfo( 'name' ) prints the site title; bloginfo( 'description' ) prints the tagline.
  • get_bloginfo() – Returns the same data as a string.
  • home_url() – The site home URL. Pass a path to build a link, for example home_url( '/contact/' ).
  • get_template_directory_uri() – The URL to the parent theme folder, used for enqueuing assets.
  • get_stylesheet_directory_uri() – The URL to the active theme folder, which differs from the template directory in a child theme.
  • the_posts_pagination() – Prints numbered pagination for the main query on archive pages.
  • the_post_navigation() – Prints previous and next post links on a single post.
  • wp_nav_menu() – Renders a registered navigation menu by its location.
  • paginate_links() – Returns pagination HTML you control, used for custom queries.

Conditional Companions

Template tags often pair with conditional tags such as is_single(), is_home(), and is_archive() to decide what to output where. Conditional tags get their own dedicated article, but remember that they let one template behave differently in different contexts.

Passing a Post ID to get_ Tags

Many get_ template tags accept a post ID as an argument, which lets them work outside the Loop where no global post is set up. This is the cleanest way to output data for a specific post you already know.

<?php
$id = 42;
echo esc_html( get_the_title( $id ) );
echo esc_url( get_permalink( $id ) );
echo esc_html( get_the_date( 'F j, Y', $id ) );

The echo-style the_ tags do not take an ID and rely on the global post, so reach for the get_ versions with an explicit ID whenever you are outside the Loop. The global post and setup_postdata article covers the full picture.

Escaping Template Tag Output

The the_ tags generally escape their own output, but the get_ tags return raw data. When you build markup by hand, escape every dynamic value:

  • esc_html() – For text shown on the page.
  • esc_attr() – For values inside HTML attributes.
  • esc_url() – For URLs in href or src.
  • wp_kses_post() – For strings that may contain allowed HTML, such as a term list.

Escaping at the point of output is a core WordPress security habit. It costs nothing and prevents cross-site scripting from unexpected data.

Best Practices

  • Prefer get_the_ versions whenever a value goes into a variable, condition, or attribute.
  • Use get_the_date() rather than the_date() inside loops to avoid the once-per-day printing quirk.
  • Escape every value from a get_ tag when you build markup yourself.
  • Check has_post_thumbnail() before printing a featured image to avoid empty tags.
  • Use get_the_term_list() for custom taxonomies rather than trying to bend the_category().
  • Call template tags inside the Loop, or pass an explicit post ID to the get_ functions that accept one.

Common Mistakes

Using the_ tags where a value is needed

Calling the_title() and trying to assign it to a variable does not work, because it echoes instead of returning. Use get_the_title(). This is the single most common template tag mistake.

Forgetting to escape get_ output

Returned values are raw. Printing get_the_title() or a meta value without esc_html() can expose the page to malformed or malicious content. Escape on output, every time.

the_date() printing nothing for some posts

the_date() suppresses repeats within the same calendar day. Developers think it is broken when several same-day posts show no date. Switch to get_the_date() for predictable output.

Calling template tags outside the Loop

Most tags read the global post set up by the Loop. Outside it, they return empty or wrong data. Pass an explicit ID to the get_ versions when you need post data outside a loop.

Troubleshooting

A template tag outputs nothing

Confirm you are inside the Loop, or that the post has the data (an empty excerpt prints nothing). For featured images, guard with has_post_thumbnail() and verify one is set in the editor.

The tag prints raw HTML as text

You probably escaped a value that already contains intended HTML with esc_html(). Use wp_kses_post() for strings that should keep safe HTML, such as a linked term list.

The date format is wrong

Pass an explicit format string to get_the_date(), for example "F j, Y". With no argument it uses the site setting, which may differ from what you expect.

Frequently Asked Questions

What is the difference between the_title() and get_the_title()?

the_title() echoes the title to the page; get_the_title() returns it as a string. Use the echo form in markup and the get form whenever you need the value in code or an attribute.

Why does the_content() show shortcodes or blocks rendered, but get_the_content() does not?

the_content() runs the content through the the_content filter, which renders shortcodes and blocks. get_the_content() returns the raw stored content. To get the filtered string, wrap it: apply_filters( "the_content", get_the_content() ).

Can I use template tags outside the Loop?

Yes, but use the get_ versions and pass an explicit post ID, for example get_the_title( $post_id ). The echo tags rely on the global post that the Loop sets up.

How do I output a custom taxonomy on a post?

Use get_the_term_list() with the taxonomy slug. It returns a linked list of terms; print it with wp_kses_post() so the links are kept and escaped.

Do I still need to escape template tag output?

Escape anything from a get_ tag when you build markup. The the_ tags usually escape for you, but raw returned values must be escaped at the point of output.

Conclusion

Template tags are the everyday vocabulary of WordPress theming. Once you internalise the the_ versus get_the_ split and the habit of escaping returned values, the dozens of individual tags become easy to use and easy to look up.

Focus on the groups: content, dates, authors, taxonomies, images, site info, and navigation. With those covered you can build almost any template, and the conditional tags article shows how to make one template adapt to every context.

Next, WordPress Conditional Tags Explained shows how to branch template logic, and the classic template hierarchy article shows which file each set of tags belongs in.