Why isn’t is_page working when I put it in the functions.php file?

I have page called “Apple”, the page’s ID id 2533.

In page.php file I have line:

echo $bannerimg 

And this function in functions.php:

if ( is_page( '2533' ) ) {    
    // also tested with 'Apple'
    $bannerimg = 'apple.jpg';

} elseif ( is_page( 'test' ) ) {    
    $bannerimg = 'test.jpg';

} elseif ( is_page( 'admissions' ) ) { 
    $bannerimg = 'admissions.jpg';

} else { 
    $bannerimg = 'home.jpg';
}  

The point is the $bannerimg echoes “home.jpg” on every page, including Apple, test and admissions.

I’ve even checked all the IDs using the_ID & $page->ID. Nothing. So I guess there’s something wrong with the code above?

8

functions.php is processed way before you can know which page is being loaded. Instead of assigning value to variable put your code into function and use that function in page.php template.

Leave a Comment