Support

Account

Home Forums General Issues offset value of type bool

Helping

offset value of type bool

  • Hello,
    I have this code:

    $image = get_field('banner_image', get_option('page_for_posts'))['url'];
    if(empty($image)){
    	$image = get_template_directory_uri().'/assets/img/breadcrumb/breadcrumb-bg-1.jpg';
    }

    and getting on the first line the php notice:
    “Trying to access array offset on value of type bool”

    I hope the following solution might be the correct way??
    I changed the first line to:

    if (!empty($image)) {
    $image = get_field('banner_image', get_option('page_for_posts'))['url'];
    }

    thanx in advance

  • The offset value notice did appear as long as the field “banner_image” is still empty.

    My solution above was just to stop the notice.

    Correct it should be like this, maybe somebody has a better way:

    if (isset(get_field('banner_image', get_option('page_for_posts'))['url'])) {
    $image = get_field('banner_image', get_option('page_for_posts'))['url'];
    }
    
    if(empty($image)){
    	$image = get_template_directory_uri().'/assets/img/breadcrumb/breadcrumb-bg-1.jpg';
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.