WordPress is_page Function [How To Use It]

WordPress is_page function

When building websites in WordPress, you may sometimes need to check which page is being rendered. There are multiple ways to do it, one of the most convenient ones is to use the WordPress’ is_page() function.

There are many use-cases for using the is_page() function, like displaying custom content only on specific pages or prohibiting specific users from accessing a page.

WordPress is_page() parameters and usage

The official definition of the WordPress is_page() function is is_page(int|string|int[]|string[] $page = ”) which means that the function can accept different kinds of parameters:

  • is_page() – basic usage without any parameter. If used this way, the function will check if any page is being displayed.
  • is_page(22) – in this example $page parameter is an integer that represents the ID of the page. The function will check if the current page that is displayed is a page with the ID = 22.
  • is_page(‘Contact’)$page parameter is a title of the page and in this scenario the function will check if the page that’s rendered is a page titled ‘Contact’
  • is_page(‘about-me’) – you can also pass a page slug to the is_page() function if you want to use the slug of the page to check against.
  • is_page([21, ‘Contact’, ‘about-me’]) – and lastly, you can an array of values to the is_page() function to check. Values in the array can be a page ID, title or a slug.

Here is how you can use the WordPress is_page() function:

<?php

if (is_page('Contact')) {
    // Display contact form if the page is Contact
}

if (is_page(22)) {
    // Page ID is 22
}

if (is_page([1, 4])) {
    // Page either has the ID of 1 or 4
}

Functions similar to WordPress is_page()

  • is_home() – checks whether the query is for the blog homepage.
  • is_front_page() – checks if the page displayed is the front page which is set in Settings -> Reading -> Your homepage displays setting on your site.
  • is_page_template(string|string[] $template = ”) – checks if the page displayed uses a specific template.
  • is_search() – determines whether a current page is a search results page.
  • is_author() – checks if the page is the author’s posts archive page.
  • is_attachment() — verifies if the page is a WordPress media attachment.
  • is_singular(string|string[] $post_types = ”) – checks if the query is for an existing single post of any post type.
  • is_single(int|string|int[]|string[] $post = ”) – determines whether the query is for an existing single post. Similar to is_page() function, but can be used with different post types as opposed to is_page() which can only be used with pages

Wrapping Up

As you can see, using WordPress is_page() function can provide you with additional flexibility when working with your code. Make sure that you use it properly and if you find yourself in a situation where you have to use the is_page() function multiple times in the same template, it may be a sign that it’s better to create another fresh template to keep your code cleaner.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

FREE TRAINING

How To Pick The Right Platform For Your Dream Course Or Membership Site

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.