WordPress glossaryA template tag is a WordPress PHP function used in theme files to display dynamic content, such as the_title() or the_permalink().
Template tags are the PHP functions a WordPress theme uses to pull content out of the database and print it on the page. Instead of typing a post's title into a template, a theme calls the_title(), and WordPress fills in the right title for whichever post is being shown. The same template can then display every post on the site.
They are called "tags" because they were designed to be dropped into HTML templates the way tags are, with little PHP knowledge needed. Under the hood, each one is an ordinary function in WordPress core.
Common template tags
- Including template parts:
get_header(),get_footer(),get_sidebar(), andget_template_part()load other theme files. - Post content:
the_title(),the_content(),the_excerpt(),the_permalink(), andthe_post_thumbnail(). - Post details:
the_author(),the_date(),the_category(), andthe_tags(). - Site information:
bloginfo( 'name' )andbloginfo( 'description' )print the site title and tagline set under Settings, then General. - Navigation:
wp_nav_menu()prints a menu, andthe_posts_pagination()prints numbered links to further pages of posts.
Print or return
Most tags come in pairs. Functions starting with the_ print their output straight to the page; their get_ counterparts return it so code can work with it first:
<h2><?php the_title(); ?></h2>
<?php $title = get_the_title(); ?>
The naming is not perfectly consistent. get_header() includes a file rather than returning anything, and bloginfo() prints while get_bloginfo() returns. The developer reference lists what each function does.
Template tags and the Loop
Many tags depend on the Loop, the block of code that steps through the posts WordPress has fetched for a page. Inside it, the_post() sets up the current post, and tags like the_title() and the_content() know which post to describe:
<?php while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
Outside the Loop, use the get_ version with a post ID, for example get_the_title( $post_id ). Site-wide tags such as bloginfo() work anywhere.
Custom template tags
Themes often define their own template tags for markup they repeat, such as a "posted on" line with the date and author. These are plain functions, usually kept in functions.php or an included file such as inc/template-tags.php. Prefix the function name with the theme's name to avoid clashes, and escape everything printed.
Escaping output
Some tags return raw data, so escape it for the place it is printed: esc_html() for text, esc_url() for links, and esc_attr() for attributes. Use the_title_attribute(), not the_title(), inside an attribute such as alt or title, because it strips tags and escapes the text.
Template tags in block themes
Block themes build templates from blocks in HTML files, so their templates do not contain PHP template tags at all. A Post Title block does the job of the_title(), and a Query Loop block replaces the Loop. Under the hood, many core blocks still render on the server by calling the same functions. Classic and hybrid themes, and plugins that output front-end markup, still use template tags every day.
Template tags, shortcodes, and the HTML template element
Template tags belong in theme files and are written by developers. Shortcodes, such as [gallery], are placed in post content by editors. And the HTML <template> element is unrelated: it holds markup that JavaScript can clone in the browser, while template tags run on the server before the page is sent.
FAQs
What is a template tag?
A template tag is a WordPress PHP function, such as the_title() or the_permalink(), that a theme calls in its template files to fetch content from the database and display it.
Can I create my own template tags?
Yes. Write a function in your theme's functions.php, or a file it includes, and call it from your templates. Give it a unique prefix and escape anything it prints. If the function should work with any theme, put it in a plugin instead.
Do template tags work outside the Loop?
Some do. Site-wide tags like bloginfo() work anywhere. Tags that describe a post, like the_title() or the_content(), need the Loop to know which post to use; outside it, call the get_ version with a post ID, such as get_the_title( $post_id ).
What's the difference between get_ and the_ functions?
Functions starting with the_ print their result directly. Their get_ counterparts return it as a value, so you can store it, change it, or escape it before printing. For example, the_title() prints the title and get_the_title() returns it.