5

I'm trying to add multiple page IDs into an if else statement. This is my code so far:

if ( is_page(ID) || is_page(ID) ) { 
    get_header('header_alt'); 
} else { 
    get_header(); 
}

I tried a few other solutions found on google, yet it always only works on the initial page after clearing the cache.

2 Answers 2

11

You can pass an array of IDs to is_page instead of using multiple is_page:

if( is_page( array( 11, 22, 33, 44 ) ) ) {
    // Your code
}

Also, if you are using it in a loop, you should consider this note:

Due to certain global variables being overwritten during The Loop, is_page() will not work. In order to call it after The Loop, you must call wp_reset_query() first.

3

To add to Jack Johansson's answer, this also works for page slugs.
For example:

if( is_page( array('news', 'work', 'about', 'contact' ) ) ) {
// Your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.