Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
I've tried with this code inside of a function as well, but seems like it returns nothing. I know putting it all into page.php should help, but is not so elegant. –  Wordpressor Jun 29 '11 at 9:01
    
I dont use id's for page reference but codex shows that the id of the page has no single quotes, have you tried that? is_page(); // When any single Page is being displayed. is_page(42); // When Page 42 (ID) is being displayed. is_page('Contact'); // When the Page with a post_title of "Contact" is being displayed. is_page('about-me'); // When the Page with a post_name (slug) of "about-me" is being displayed. is_page(array(42,'about-me','Contact')); // Returns true when the Pages displayed is either post ID 42. probably not the case but worth a shot –  MartinJJ Jun 29 '11 at 9:09
1  
@Martin quotes are not entirely correct, but won't break anything either - it is loosely compared by default (without checking if type matches) –  Rarst Jun 29 '11 at 10:42

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';
    }  
}
share|improve this answer

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 up page-apple.php

share|improve this answer
    
is_page() can accept ID as integer as well –  Bainternet Jun 29 '11 at 9:41
    
yeah but an int shouldn't be quoted –  Alex Older Jun 29 '11 at 9:51
    
That's strange, because I see quotes everywhere: codex.wordpress.org/Conditional_Tags –  Wordpressor Jun 29 '11 at 12:45
    
and it's a bad practice. –  Alex Older Jun 29 '11 at 15:12

You need to call your function at a point in the WordPress process after the Query is set up.

In functions.php:

function mytheme_get_banner_img() {
    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';
    }  
    return $bannerimg;
}

Then, in your page.php template file, wherever you need to return/output $bannerimg:

<?php
$bannerimg = mytheme_get_banner_img();
?>

Then, you can do whatever you need to with $bannerimg: drop it in an <img> tag, etc.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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