3

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.

8

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, privacy policy and cookie policy

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