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:
- Without Parameters: If you use
is_page()
without any parameters, it will returntrue
if any page is being displayed.
if ( is_page() ) { // Some code here will run if any page is being displayed }
- 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 }
- 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.