Did you know that you can add and show custom content on specific pages, of your WordPress site, without having to create a new code template? By using the conditional WordPress is_page() tag, you can command the existing code to act in various ways on different pages.
In this article, I will cover the function of conditional tags and how is_page() works.
Introduction to WordPress Conditional Tags
As the name suggests, this tag is one of many WordPress functions that allows you to control your content according to certain criteria.
It’s a boolean data type that works by checking whether or not the situation is TRUE or FALSE — it’ll return as true if the pages meet the condition of the tags and vice versa.
You can make your website pages perform specific commands using this function without having to create separate templates for each page. It’ll work as long as the tag comes out as true — useful for calling additional content for different pages simultaneously.
To help you understand it better, here’s an example of a conditional tag in a basic format that would also work with the WordPress is_page() tag.
IF (THE CRITERIA IS TRUE) { DO THIS COMMAND }
Replace “THE CRITERIA IS TRUE” with the conditional tag you want to use.
Pretty straightforward, right?
Popular Conditional Tags You Might Need
There are many conditional tags available on the WordPress codex that you can use. They follow the basic function of is_criteria(). Uniquely, the “criteria” is replaced by the targeted pages of your website. The same goes for the WordPress is_page() tag.
Here’re some of the widely used ones.
- is_home() — asks, “Is it displaying the blog index page/home page?”
- is_front_page() — confirms whether or not the front page of the website is shown.
- is_page() — ensures that a page is being opened.
- is_attachment() — verifies if the visitors open an attachment file.
- is_search() — makes sure that a search result page is displayed.
- is_author() — checks whether or not the author archive page is shown.
How Does the is_page() Conditional Tag Work?
A WordPress website contains a collection of pages. Therefore, it’s essential to understand how the is_page() conditional tag works in order to customize page content.
is_page() inspects if a page is being opened. You can use this tag to manage your content on even more specific pages.
Use it to customize any non-time-chronological pages — such as your About, Contact, Copyright, Company Information pages, etc. Whether to display a logo, deliver a message, recall existing template files, or command certain actions.
To do that, you’ll need to structurize the tag as follow:
is_page( int|string|array $page = '' ) { THE COMMAND }
Specify the $page parameters between the brackets with the page you want to customize. Fill it with the page’s slug, ID, title, or array. Then, replace “THE COMMAND” with whatever new content you want to display on that page.
Let’s say that you want to check your About page and display a custom message to your visitors. The tag code should look like this:
<?php if (is_page(‘About’)) { echo "This page tells you about the company’s history..."; } ?>
The message will appear only when your audience visits the About page, not when they open the Contact or Sitemap page, and so on.
Check the WordPress developer’s site for more brief references.
Wrapping Up
As you can see, the WordPress conditional tag is a function to display additional content or changes according to certain criteria.
There are many conditional tags available. However, all of them share the same basic structure:
IF (THE CRITERIA IS TRUE) { DO THIS COMMAND }
Fill “THE CRITERIA” with the page you’re about to control, for instance is_home(), is_front_page(), is_page(), is_attachment(), is_search(), and is_author().
If you want to customize the content of a specific page on your WordPress site, use the is_page() tag. Specify the $page parameters, and define what content you would like to display with this code:
is_page( int|string|array $page = '' ){ THE COMMAND }
So, what are you waiting for? It’s time to use conditional tags and is_page() to make the best of your WordPress site!
Leave a Reply