Wordpress - Why isn't is_page working when I put it in the functions.php file?
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.
get_header
should work if you want to leave it in functions.php
add_action('get_header', function() {
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';
}
});
Extending what @Rarst posted and you commented , a more elegant solution would be to create your own filter inside page.php and hook to it from a function inside the functions.php, for example:
in you page.php
$bannerimg = apply_filters('my_bannerimg','defualt_img.jpg');
and in your functions.php
add_filter('my_bannerimg','what_page_is_it');
function what_page_is_it($img){
if ( is_page( '2533' ) ) {
return 'apple.jpg';
} elseif ( is_page( 'test' ) ) {
return 'test.jpg';
} elseif ( is_page( 'admissions' ) ) {
return 'admissions.jpg';
} else {
return 'home.jpg';
}
}