为什么我的PHP函数代码会出现未定义的索引错误

问题描述 投票:0回答:1

我什至尝试使用“ isset”,但遗憾的是它不起作用。

function pageBanner($args=NULL){
if(!$args['title']){
    $args['title']=get_the_title();
}

if (!$args['subtitle']){
    $args['subtitle'] = get_field('page_banner_subtitle');
}

if (!$args['photo']){
    if (get_field('page_banner_background_image')){
        $args['photo'] = get_field('page_banner_background_image')['sizes']['pageBanner'];
    }
    else {
        $args['photo']=get_theme_file_uri('images/pages.jpg');
    }
}?>

我不知道if(!$args['title']){ $args['title']=get_the_title();上的问题是否有效,但字幕和照片均未定义索引。

php wordpress function isset
1个回答
0
投票

尝试这样的事情:

function pageBanner($args = null)
{
    if ($args === null) {
        $args = [];
    }

    if (!isset($args['title'])) {
        $args['title'] = get_the_title();
    }

    if (!isset($args['subtitle'])) {
        $args['subtitle'] = get_field('page_banner_subtitle');
    }

    if (!isset($args['photo'])) {
        $field = get_field('page_banner_background_image');
        if ($field && isset($field['sizes']['pageBanner'])) {
            $args['photo'] = $field['sizes']['pageBanner'];
        } else {
            $args['photo'] = get_theme_file_uri('images/pages.jpg');
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.