Template Tags
Template tags are PHP functions you call in theme files to print content from the current post or query. the_title(), the_content(), the_permalink(), wp_nav_menu() — they make WP themes feel like a templating language.
The most-used tags + safe rendering
EXAMPLE
<?php
// 1) Inside the Loop — current post
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="meta">
Posted <?php the_time('F j, Y'); ?> by <?php the_author(); ?>
in <?php the_category(', '); ?>
</p>
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('medium_large'); ?>
<?php endif; ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<p><a href="<?php the_permalink(); ?>">Read more</a></p>
</article>
<?php
// 2) The Get-variants — return instead of print
$title = get_the_title(); // string
$url = get_permalink(); // string
$id = get_the_ID(); // int
$cats = get_the_category(); // array of WP_Term
$thumb = get_the_post_thumbnail_url(null, 'large');
?>
<!-- 3) Conditional tags — what page is this? -->
<?php
if (is_home()) { /* blog index */ }
if (is_front_page()) { /* set static front page */ }
if (is_single()) { /* single post */ }
if (is_page('about')) { /* the About page */ }
if (is_archive()) { /* archives */ }
if (is_category('news')) { /* News category archive */ }
if (is_singular('product')) { /* single CPT */ }
if (is_user_logged_in()) { /* logged-in */ }
?>
<!-- 4) Navigation -->
<?php
wp_nav_menu([
'theme_location' => 'primary',
'container' => 'nav',
'menu_class' => 'main-nav',
'depth' => 2,
]);
?>
<!-- 5) Comments -->
<?php
if (comments_open() || get_comments_number()) {
comments_template();
}
?>
<!-- 6) Body class + head/foot hooks (DO NOT FORGET) -->
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<?php // …content… ?>
<?php wp_footer(); ?>
</body>
<!-- 7) Safe-output for unknown text/HTML -->
<?php echo esc_html($title); ?> <!-- text -->
<?php echo esc_attr($css_class); ?> <!-- attribute -->
<?php echo esc_url($url); ?> <!-- href/src -->
<?php echo wp_kses_post($rich_html); ?> <!-- limited HTML allowed -->
Why it matters
the_* prints; get_the_* returns. Mixing them up is the #1 source of double-output and weird stripped tags in custom themes — pick the right half of the pair.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
the_title() the_content() the_excerpt() the_permalink() the_post_thumbnail() get_the_date()Try it Yourself »
Discussion
Loading…