-1

I even tried using "isset", but sadly it is not working.

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

I didn't know the problem on my if(!$args['title']){ $args['title']=get_the_title(); it is working, but the subtitle and photo are undefined index.

bonbon
  • 11
  • 2
  • 4
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Jeto Dec 12 '19 at 23:36

2 Answers2

3

Try something like this:

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');
        }
    }
}
Aran
  • 2,429
  • 1
  • 19
  • 19
  • 2
    It works, thank you, but what seems to be the problem? – bonbon Dec 12 '19 at 23:49
  • @bonbon You didn't use `isset` as you said you had? – Jeto Dec 13 '19 at 00:11
  • 1
    You had 4 uses of `isset()` missing which could have potentially thrown an error if undefined. Also, if `$args` was `null` then when trying to append your also get issues because you'd never created it as an array. This solved each of those issues for you – Aran Dec 13 '19 at 07:15
1

Well, you really don't need to change anything other than adding isset() on each loop.

What I meant:

function page_banner ($args = null) {

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

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

if (!isset($args['photo'])){
    if ((get_field('page_banner_background_picture'))) {
        $args['photo'] = get_field ('page_banner_background_picture') ['sizes'] ['pagebanner'];
    } else {
        $args['photo'] = get_theme_file_uri('/images/ocean.jpg');
    }
}

This works just fine.

Setting an args=null is also enough, I mean:

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

not having this doesn't throw an error message.

goku
  • 23
  • 7