Beginners Guide To Conditional Tags in WooCommerce and WordPress

Introduction

The Conditional Tags allows the developers to manage what and how the content is displayed on their website. Whether you are developing a theme for WordPres or WooCommerce store, it is great to use these conditional tags to avoid complicated checks in your code. Using the boolean (True/False) conditions, it becomes super easy to display the data you want. The function names are easy to remember. For instance, you can set conditions in your code to show different content according to the device used by the visitor.

In this short guide, you can find the entire set of conditional tags and how to use them in your code.

Understanding the PHP ‘If’ Statements

The PHP ‘If’ statements are used for verifying conditional statements for True (1) and False (0) results. The ‘elseif’ allows the developers to use additional statements. The ‘else’ option will be executed, if the prior statement result is false.

The most useful conditional tags in WooCommerce


if ( is_woocommerce() ) {
  echo 'This is a WooCommerce page.';
}

Determines whether or not you are on any kind of WooCommerce page e.g. product, product category, cart, etc.


if ( is_shop() ) {
  echo 'This is the main shop page';
}

Checks whether the product archive page is displayed.


if ( is_product_category() ) {
  echo 'This is a product category page.';
}

This function checks whether a product category archive is displayed.


if ( is_product() ) {
  echo 'This is a product page.';
}

Determines if the current page is a product page.


if ( is_cart() ) {
  echo 'This is the WooCommerce cart page.';
}

Determines if the current page is cart page.


if ( is_checkout() ) {
  echo 'This is the WooCommerce checkout page';
}

Determines if the current page is checkout page.

Conditional tags for WordPress


if ( is_page_template( 'about.php' ) {
  echo 'You are currently on about.php template';
} elseif ( is_front_page() ) {
  echo 'You are on front page.';
} else {
  echo 'Not a front page and not about.php template page.';
}

This function helps you to find whether you are currently on a template page you’re checking. You need to specify the teplate name you want to check within the brackets.

In the abovementioned example, if the user is on ‘about.php’ template page, the result will show true. When the condition on the ‘if’ statement is found false, it will verify the ‘elseif’ statement. If the user is not on the ‘Front page’, then the ‘else’ statement will be executed.

Checking Multiple Statements

The Conditional tags is also used for checking two or more statements using ‘AND’ / ‘OR’ to find if a certain condition is met or not. Depending on the test outcome it returns the result as TRUE or FALSE. The result either TRUE or FALSE determines what code will take action.

The most used conditional tags in WordPress

Following is a list of some of the most popular conditional tags.

Note: The exact functionality of some conditional tags may differ from their names.


if ( is_home() ) {
  echo 'Current page is home page.';
}

It is used to check if the current page is home page.


if ( is_front_page() ) {
  echo 'You are viewing Front page.';
}

When you set a static page as the front page on the website, this conditional tag helps to determine whether or not you are currently on the front page.


if ( is_single() ) {
  echo 'This is a single post page';
}

Helps you to identify whether you are a single post.


if ( is_attachment() ) {
  echo 'You are viewing an attachment';
}

Helps to find if the current post is an attachment (media file).


if ( is_page( 'about-us' ) ) {
  echo 'This is about us page.';
}

Checks to find out if the query is a single page. When specified with page name argument, it also determines if you’re on that page.


if ( is_singular() ) {
  echo 'A singular post.';
}

Determines if you’re on a singular post of any post type.


if ( is_category() ) {
  echo 'You are on a category page';
}

Determines and returns true when category archive page is displayed.


if ( is_search() ) {
  echo 'This is a search page.';
}

Determines if the search result archive page is displayed.


if ( is_archive() ) {
  echo 'This is a category page.';
}

Determines if any type of archive page is displayed – tag, category, taxonomy term.

Leave a Reply

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