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?
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 inpage.php
template.get_header
should work if you want to leave it infunctions.php
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
and in your functions.php
Add this to your functions.php, change name of script someCode and name of page:
In functions.php
is_page()
with theadd_action('wp', 'your_function_name');
works fine.You need to call your function at a point in the WordPress process after the Query is set up.
In
functions.php
:Then, in your
page.php
template file, wherever you need to return/output$bannerimg
:Then, you can do whatever you need to with
$bannerimg
: drop it in an<img>
tag, etc.Have you correctly declared
wp_head();
etc in your theme?Also,
is_page
accepts an ID without quotes.The problem may also be the fact you are already on the page template so it is a page, you may be better of querying the
$post->ID
or set uppage-apple.php
this will definitely work on page.php file please move there and check.
[NB]: // replace your page id | slug | array with Vehicle
You need to hook the
wp
function.Important note: Note that if you hook to the
init
function, is_page will not work