Conditional and Template Tags

Summary: Learning about different Conditional and Template Tags in WordPress.

Conditional Tags

Conditional Tags can be used in our Template Files in classic themes to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions. Conditional Tags usually work with PHP if /else Conditional Statements.

The code begins by checking to see if a statement is true or false. If the statement is found to be true, the first set of code is executed. If it’s false, the first set of code is skipped, and the second set of code after the else is executed instead.

For a Conditional Tag to modify our data, the information must already have been retrieved from our database the query must have already run. If we use a Conditional Tag before there is data, there’ll be nothing to ask the if/else statement about.

is_home()

This condition returns true when the main blog page is being displayed, usually in standard reverse chronological order. If our home page has been set to a Static Page instead then this will only prove true on the page which we set as the “Posts page” in Settings > Reading.

is_front_page()

This condition returns true when the front page of the site is displayed, regardless of whether it is set to show posts or a static page.

is_admin()

This condition returns true when the Dashboard or the administration panels are being displayed.

is_single()

Returns true when any single Post or attachment, or custom Post Type is being displayed. This condition returns false if we are on a page.

is_single( '17' );
is_single( 'Irish Stew' );
is_single( 'beef-stew' );
is_single( array( 17, 'beef-stew', 'Irish Stew' ) );
is_single( array( 17, 19, 1, 11 ) );

is_singular()

Returns true for any is_single, is_page, and is_attachment. It does allow testing for post types.

is_sticky()

Returns true if the “Stick this post to the front page” check box has been checked for the current post.

get_post_type()

We can test to see if the current post is of a certain type by including get_post_type() in our conditional. It’s not really a conditional tag, but it returns the registered post type of the current post.

if ( 'rt-movie' == get_post_type() ) { ... }

is_page()

This section refers to WordPress Pages not any generic webpage from our blog or in other words to the built in post_type ‘page’.

is_page( '42' );
is_page( 'About Me And Joe' );
is_page( 'about-me' );
is_page( array( 42, 'about-me', 'About Me And Joe' ) );

is_page_template()

Allows us to determine whether or not we are in a page template or if a specific page template is being used.

is_page_template( '404.php' );

is_category()

When a Category archive page is being displayed.

is_category( '9' );
is_category( 'Stinky Cheeses' );
is_category( 'blue-cheese' );
is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );

is_tag()

When any Tag archive page is being displayed.

is_tag( 'mild' );
is_tag( array( 'sharp', 'mild', 'extreme' ) );

is_tax()

When any Taxonomy archive page is being displayed.

is_tax( 'flavor' );
is_tax( 'flavor', 'mild');

is_author()

When any Author page is being displayed.

is_author( '4' );
is_author( 'Vivian' );

is_singular()

When any of the following return true: is_single()is_page() or is_attachment().

Template Tags

In WordPress template tags are PHP functions used in themes to display dynamic information like posts, pages, categories, custom fields, and more.

bloginfo()

Displays information about the blog, such as the name, URL, description, and more.

  • Parameter: $show defines what information to display ('name', 'description', 'url').
  <h1><?php bloginfo( 'name' ); ?></h1>
  <p><?php bloginfo( 'description' ); ?></p> 

get_header()

Loads the header.php template file.

  <?php get_header(); ?>

Loads the footer.php template file.

  <?php get_footer(); ?>

get_sidebar()

  <?php get_sidebar(); ?>

the_title()

Displays the title of the current post or page.

  <h2><?php the_title(); ?></h2>

the_content()

Displays the content of the current post or page.

  <div><?php the_content(); ?>

the_excerpt()

Displays a summary or excerpt of the current post.

  <p><?php the_excerpt(); ?></p>

Returns the permalink (URL) of the current post or page.

  <a href="<?php the_permalink(); ?>">Read More</a>

the_post_thumbnail()

Displays the featured image of the current post.

  <div><?php the_post_thumbnail(); ?></div>

Displays a custom navigation menu defined in the WordPress dashboard.

  • Parameter: $args is an array of arguments for the menu.
  <?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?> 

Outputs necessary HTML, scripts, styles, and meta information in the <head> section. Should be used just before the closing </head> tag in our theme.

  <head>
    <?php wp_head(); ?>
  </head>

Outputs necessary scripts and other elements before the closing </body> tag. Should be placed just before the </body> tag.

  <body>
    <?php wp_footer(); ?>
  </body>

the_category()

Displays the categories of the current post.

  <p><?php the_category( ', ' ); ?></p>

the_tags()

Displays the tags of the current post.

  <p><?php the_tags(); ?></p>

get_template_part()

Loads a specific template part (header, footer, post template).

  <?php get_template_part( 'content', 'single' ); ?> 

comments_template()

Includes the comments template for the current post or page.

  <?php comments_template(); ?>

edit_post_link()

Displays an edit link for logged-in users who have permission to edit the post.

  <?php edit_post_link( 'Edit this post' ); ?>

the_author()

Displays the author of the current post.

  <p>Written by <?php the_author(); ?></p>

is_home()

Conditional tag that checks if the current page is the blog’s home page.

  <?php if ( is_home() ) : ?>
    <h1>Welcome to the Blog</h1>
  <?php endif; ?>

is_single()

Conditional tag that checks if the current page is a single post.

  <?php if ( is_single() ) : ?>
    <h2>This is a Single Post</h2>
  <?php endif; ?>
«
»

Leave a Reply

Your email address will not be published. Required fields are marked *