How to Use the “if (is_page())” Conditional Statement in WordPress?

In WordPress, is_page() is a conditional tag that allows you to check if a certain page is being displayed. This can be useful if you want to add or remove elements or apply styles to specific pages.

You can use is_page() in three ways:

  1. Without Parameters: If you use is_page() without any parameters, it will return true if any page is being displayed.
if ( is_page() ) {
    // Some code here will run if any page is being displayed
}
  1. With Page ID: You can pass the ID of the page as a parameter to check if a specific page is being displayed.
if ( is_page( 42 ) ) {
    // Some code here will run if the page with the ID of 42 is being displayed
}
  1. With Page Title or Slug: You can also pass the title or slug of the page as a parameter.
if ( is_page( 'Contact Us' ) ) {
    // Some code here will run if the page titled 'Contact Us' is being displayed
}

or

if ( is_page( 'contact-us' ) ) {
    // Some code here will run if the page with the slug 'contact-us' is being displayed
}

Remember to replace the page ID, title, or slug with your own. Also, ensure the conditional tags are placed within the loop in your theme files, or they may not work as expected.

Leave a Comment