Checking if current page is a WordPress page using is_page

The WordPress is_page function checks whether a specific page is being displayed and returns a boolean value. This can be useful for conditional logic in WordPress themes or plugins, allowing developers to customize the display or behavior of a page based on its ID or slug.

  • For example, a developer could use the is_page function to conditionally load additional scripts or stylesheets only on specific pages, improving performance and reducing unnecessary code execution.
  • Additionally, the function can be used to display different content or layout for specific pages, creating a more tailored user experience.

The is_page function provides a way to dynamically control the behavior and appearance of WordPress pages based on their individual characteristics.

Parameters Accepted by the WordPress is_page Function

  • $page (int, string, int[], string[]), optional. Default value: ”. Description: Page ID, title, slug, or array of such to check against.

Value Returned by the WordPress is_page Function

The function returns a boolean value, indicating whether the query is for an existing single page.

Examples

How to check if the current page is a specific page using is_page function

Below is an example of using the is_page function to check if the current page is a specific page:

if ( is_page( 'about-us' ) ) {
 // Do something if the current page is the 'about-us' page
}

This code snippet uses the is_page function to check if the current page is the ‘about-us’ page. If it is, it will execute the code inside the if statement.

How to check if the current page is a child of a specific page using is_page function

Below is an example of using the is_page function to check if the current page is a child of a specific page:

if ( is_page( 'services' ) && $post->post_parent == 5 ) {
 // Do something if the current page is a child of the 'services' page
}

This code snippet uses the is_page function to check if the current page is the ‘services’ page and also checks if the current page’s parent is page ID 5. If both conditions are true, it will execute the code inside the if statement.

How to check if the current page is not a specific page using is_page function

Below is an example of using the is_page function to check if the current page is not a specific page:

if ( ! is_page( 'contact' ) ) {
 // Do something if the current page is not the 'contact' page
}

This code snippet uses the is_page function with the negation operator to check if the current page is not the ‘contact’ page. If it is not, it will execute the code inside the if statement.

Conclusion

The is_page function is a valuable tool for developers working with WordPress. It provides a simple and efficient way to check if a specific page is being displayed, allowing for conditional logic and dynamic content. By understanding how to properly use this function, developers can enhance the functionality and user experience of their WordPress websites.

Whether it’s displaying different content based on the current page or executing specific actions only on certain pages, the is_page function offers a versatile solution. With its ability to accept various parameters and options, it provides developers with the flexibility to tailor their code to specific page contexts.

The is_page function is an essential tool that can greatly enhance the development process for WordPress websites.

Related WordPress Functions