Using WordPress ‘is_page()’ PHP function

The is_page() WordPress PHP function determines whether the query is for an existing single page. It can also check if the query is for one of the specified pages if the $page parameter is provided.

Usage

is_page($page)

Example:
Input: is_page('Contact')
Output: true if the page with a title ‘Contact’ is being displayed

Parameters

  • $page (int|string|array) (Optional) – Page ID, title, slug, or array of such to check against. Default: ”

More information

See WordPress Developer Resources: is_page()

Examples

Check if any single page is being displayed

if (is_page()) {
  // Any single page is being displayed
}

Check if a specific page by ID is being displayed

if (is_page(42)) {
  // Page with ID 42 is being displayed
}

Check if a specific page by title is being displayed

if (is_page('Contact')) {
  // Page with title 'Contact' is being displayed
}

Check if a specific page by slug is being displayed

if (is_page('about-me')) {
  // Page with slug 'about-me' is being displayed
}

Check if any of the specified pages are being displayed

if (is_page(array(42, 'about-me', 'Contact'))) {
  // Either page with ID 42, slug 'about-me', or title 'Contact' is being displayed
}