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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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';
    }  
});

Method 3

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';
    }  
}

Method 4

Add this to your functions.php, change name of script someCode and name of page:

   add_action('wp_enqueue_scripts', 'wpt_theme_js');

    function wpt_theme_js() { 
        if ( is_page('somePage') ) {
            wp_enqueue_script('someCode_js', get_template_directory_uri() . '/js/someCode.js', '', '', true);
        }
    }

Method 5

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.

Method 6

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

Method 7

In functions.php is_page() with the add_action('wp', 'your_function_name'); works fine.

Method 8

if( is_page('Vehicle') ) {
    // code here
}

this will definitely work on page.php file please move there and check.

[NB]: // replace your page id | slug | array with Vehicle


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x