I’ve built a WordPress website based on the Genesis Framework. A custom function is present on the functions.php, see below, and I’m getting the error messages, also below, when updating the server’s PHP from 7.4 to 8.X. Looking to upgrade the website’s PHP to 8.0 for security and speed functionality and display the custom featured image function on the front end of the website.
The website is locally being worked on, no link can be provided.
WARNING: UNDEFINED VARIABLE $PAGE IN FUNCTIONS.PHP
WARNING: ATTEMPT TO READ PROPERTY "ID" ON NULL IN FUNCTIONS.PHP
WARNING: UNDEFINED VARIABLE $PAGE IN FUNCTIONS.PHP
WARNING: ATTEMPT TO READ PROPERTY "ID" ON NULL IN FUNCTIONS.PHP
WARNING: UNDEFINED VARIABLE $PAGE IN FUNCTIONS.PHP
WARNING: ATTEMPT TO READ PROPERTY "ID" ON NULL IN FUNCTIONS.PHP
//Add hero image above post/page content
// Create new image size for our hero image
add_image_size( 'hero-image', 1400, 400, TRUE ); // creates a hero image size
// Hook after header area
add_action( 'genesis_after_header', 'bw_hero_image' );
function bw_hero_image() {
// If it is a page and has a featured thumbnail, but is not the front page do the following...
if (has_post_thumbnail() && is_page() && !is_front_page() &&!(is_page('about-peter')) ) {
// Get hero image and save in variable called $background
$image_desktop = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'hero-image' );
$image_tablet = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'large' );
$image_mobile = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'medium' );
$bgdesktop = $image_desktop[0];
$bgtablet = $image_tablet[0];
$bgmobile = $image_mobile[0];
$mobile_width = $image_mobile[1];
$mobile_height = $image_mobile[2];
$featured_class = 'above-post-hero';
?>
<div class='<?php echo $featured_class; ?>'></div>
<style>
<?php echo '.'.$featured_class.' { margin: auto; background-image:url( '.$bgmobile.'); height:'.$mobile_height.'px; width: '.$mobile_width.'px; } '; ?>
@media only screen and (min-width : 480px) {
<?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgtablet;?>);height:276px;width:100%}
}
@media only screen and (min-width : 992px) {
<?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgdesktop;?>);height:400px;}
}
</style>
<?php
}
}
Tried adding the is_page() function, inside the bw_hero_image function, with an array of the pages not displaying the featured image, did not help. Attempted to add the is_page() function with the page’s slug as a string for the featured image, did not work.
When performing that last attempt, the error message disappears but the feature image are not displayed on the pages.
2
Answers
this code to change defute Genesis Framework seeting from title hero to header/background image using featured image:
The error message says that you are missing the
$page
variable.In this particular case, you shouldn’t be using
$page
but$post
and use it from the global scope.Another solution is to call the
get_post_thumbnail_id()
with no parameter which will default to the currentglobal $post