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
trueorfalsefor the request being shown: "is this the front page?", "is this a category archive?". You use them inifstatements.
The previous lesson covered the tags used inside the Loop. Here are the ones that work anywhere.
Site information
<?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";My WordPress Site
http://localhost:8080/
http://localhost:8080/wp-login.php
http://localhost:8080/wp-admin/edit.php
en-US
7.1.3bloginfo( '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
/*
Theme Name: Tags Demo
Version: 1.0.0
*/<?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>';wp theme activate tags-demoSuccess: Switched to 'Tags Demo' theme.Each address now reports what WordPress thinks it is:
GET /front pageGET /hello-world/single postGET /sample-page/pageGET /category/uncategorized/category archiveGET /?s=hellosearch resultsGET /missing/not foundThe 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():
<article class="card">
<h2><?php echo esc_html( get_the_title() ); ?></h2>
</article><?php
while ( have_posts() ) {
the_post();
get_template_part( 'parts/card' );
}GET /hello-world/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.
Navigation tags
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 (useis_page()oris_singular()). - Calling
is_*()tags too early, for example from theinithook, before the query has run. Usewpor later, or the template itself. - Echoing
get_*functions withoutecho, orechoingthe_*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
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.