Himanshu SharmaSoftware engineer
Let’s talk

WordPress · Lesson 26 of 45

Template tags and conditional tags

Print site and post data with template tags, and use conditional tags like is_front_page() and is_single() to change output by page type.

12 minClassic templates and queriesUpdated 23 October 2026

By the end of this lessonPrint the right data in templates, choose between echoing and returning functions, and use conditional tags to change output by page type.

Two kinds of tags

Classic themes build pages out of PHP functions. There are two groups:

  • Template tags print or return data: the site name, a post's title, a menu, a pagination bar.
  • Conditional tags return true or false for the request being shown: "is this the front page?", "is this a category archive?". You use them in if statements.

The previous lesson covered the tags used inside the Loop. Here are the ones that work anywhere.

Site information

php
<?php
echo get_bloginfo( 'name' ), "\n";
echo get_bloginfo( 'description' ), "\n";
echo home_url( '/' ), "\n";
echo site_url( '/wp-login.php' ), "\n";
echo admin_url( 'edit.php' ), "\n";
echo get_bloginfo( 'language' ), "\n";
echo get_bloginfo( 'version' ), "\n";
result
My WordPress Site

http://localhost:8080/
http://localhost:8080/wp-login.php
http://localhost:8080/wp-admin/edit.php
en-US
7.1.3

bloginfo( 'name' ) prints, get_bloginfo( 'name' ) returns. For addresses, use the URL functions rather than writing a domain by hand: home_url() is the address visitors use, site_url() is where WordPress itself is installed (usually the same), and admin_url() is for the dashboard. In theme files use get_theme_file_uri() for files in your theme.

Conditional tags

css
/*
Theme Name: Tags Demo
Version: 1.0.0
*/
php
<?php
$kind = 'something else';
if ( is_front_page() )      { $kind = 'front page'; }
elseif ( is_home() )        { $kind = 'blog page'; }
elseif ( is_single() )      { $kind = 'single post'; }
elseif ( is_page() )        { $kind = 'page'; }
elseif ( is_category() )    { $kind = 'category archive'; }
elseif ( is_search() )      { $kind = 'search results'; }
elseif ( is_404() )         { $kind = 'not found'; }
echo '<p class="kind">', esc_html( $kind ), '</p>';
bash
wp theme activate tags-demo
result
Success: Switched to 'Tags Demo' theme.

Each address now reports what WordPress thinks it is:

http
GET /
result
front page
http
GET /hello-world/
result
single post
http
GET /sample-page/
result
page
http
GET /category/uncategorized/
result
category archive
http
GET /?s=hello
result
search results
http
GET /missing/
result
not found

The most used conditional tags:

Tag True when
is_front_page() the site's front page (posts or a static page)
is_home() the blog posts page
is_singular() any single post, page or custom post type
is_single() a single post or custom post type, not a page
is_page( 'about' ) a page, optionally a specific slug, ID or title
is_category(), is_tag(), is_tax() a category, tag or custom taxonomy archive
is_archive() any archive: category, tag, author, date, post type
is_search(), is_404() search results, or no page found
is_user_logged_in() the visitor is logged in
is_admin() the dashboard is being shown, not the front end

Static front pages and is_home()

The names confuse everyone once. When the site shows latest posts on the front page, is_front_page() and is_home() are both true on /. When you choose a static page as the front page and another page for posts (Settings → Reading), is_front_page() is true on the static page and is_home() is true on the posts page. Test them on a real site before you rely on them.

Reusing pieces of markup

To avoid copying markup between templates, put it in a file and load it with get_template_part():

php
<article class="card">
	<h2><?php echo esc_html( get_the_title() ); ?></h2>
</article>
php
<?php
while ( have_posts() ) {
	the_post();
	get_template_part( 'parts/card' );
}
http
GET /hello-world/
result
Hello world!

A child theme can replace parts/card.php with its own copy, and you can pass a second argument (get_template_part( 'parts/card', 'wide' ) loads parts/card-wide.php) or a third array of variables, which the file reads as $args.

For lists of posts, the tags the_posts_pagination() (numbered pages), the_posts_navigation() (older / newer links) and the_post_navigation() (previous / next post on a single page) print navigation with the right links and screen-reader text. Menus use wp_nav_menu(), which you meet in Navigation menus.

Escape what you print

Template tags such as the_title() already return safe, filtered values. Whatever you build yourself must be escaped when you print it: esc_html() for text, esc_attr() for attribute values, esc_url() for addresses. The conditional above does this with esc_html( $kind ).

Common mistakes

  • Using is_home() when you mean the front page.
  • Using is_single() for pages (use is_page() or is_singular()).
  • Calling is_*() tags too early, for example from the init hook, before the query has run. Use wp or later, or the template itself.
  • Echoing get_* functions without echo, or echoing the_* functions.

Exercise

Change index.php so that it prints "Welcome back" only to logged-in visitors, on the front page.

Show one possible answer
php
<?php
if ( is_front_page() && is_user_logged_in() ) {
	echo '<p>Welcome back, ', esc_html( wp_get_current_user()->display_name ), '</p>';
}

Combine the tags with &&. wp_get_current_user() returns the logged-in user.

Key points

  • Template tags are functions that print data: site info (bloginfo, home_url), post data (the_title) and navigation (the_posts_pagination).
  • Conditional tags such as is_front_page(), is_home(), is_single(), is_page(), is_category() and is_search() return true or false for the current request.
  • is_home() is the blog posts page, which is not the same as the front page when a static page is the front page.
  • Use get_template_part() to reuse parts of a template, and always escape values you print yourself.

Check yourself

1. A site shows a static page as its front page and posts on /blog/. On /blog/, which conditional tag is true?
2. Which function returns the site name without printing it?
3. What does get_template_part( 'parts/card' ) load?
4. Which tag is true on a single page of the 'page' post type?