0

I have inherited a problematic website after communication broke-down with their original web developer.

I have been working through and resolving as many errors as possible.

PHP Notice: Undefined offset: 0 in /home/customer/www/public_html/wp-content/themes/modus-child/functions.php on line 51

The long term goal will be to re-build the website, using a different theme, but I would appreciate some help in resolving this error to give us a safety net while the development is taking place.

Screenshot showing the code as pasting the code didn't look right

here is line 46 onwards Line 51 is the line that begins with if($featured[0]=="Featured"){ I can see the offset 0 in brackets.

$count=0;
      foreach ($woo_catgery as $sc) {
            $count++;
            if($count==6){continue;}
            $featured = get_field('featured', $sc->taxonomy . '_' . $sc->term_id);
            if($featured[0]=="Featured"){
            $thumbnail_id = get_term_meta( $sc->term_id, 'thumbnail_id', true );
            $image_url=wp_get_attachment_url( $thumbnail_id ); 
            if(!empty($image_url)){
                $image_url=$image_url;
            }else{$image_url=get_stylesheet_directory_uri().'/img/blank.png';}
            $link = get_term_link( $sc->slug, $sc->taxonomy );
            $hm .='<li>
                    <div class="cat_image">
                        <a href="'. $link .'" title="" target="_self">        
                            <img alt="icon" src="'.$image_url.'">
                        </a>                    
                    </div>
                    <div class="cat_bx_cnt">
                        <div class="cat_ttl">
                            <h3 class="cat_ttl_mn">'.$sc->name.'</h3>                      
                        </div>
                        <a class="link-more" href="'. $link .'" title="" target="_self">
                            <i class="fa fa-angle-right" aria-hidden="true"> </i>
                        </a>
                    </div>
                </li>               
            '; 
            }
      }
      $hm .='</ul>';
      return $hm;
    
}
add_filter( 'woocommerce_marketing_menu_items', '__return_empty_array' );

add_action( 'after_setup_theme', 'remove_pgz_theme_support', 100 );

function remove_pgz_theme_support() { 
remove_theme_support( 'wc-product-gallery-zoom' );
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • The error means, that `$featured` did not contain any element under an index/key `0`. We don’t know what kind of value the script is trying to read there, so we don’t know what `get_field` is _supposed_ to return here. It might have simply returned null, if no value was stored for that field in the first place – in that case, and if any of the further processing is to be skipped then, replacing that line with `if($featured && $featured[0]=="Featured")` could already do the trick. – CBroe Mar 08 '21 at 15:05
  • Not a Wordpress guy, but from the manual `get_field()` returns a scalar value no an array – RiggsFolly Mar 08 '21 at 15:13

1 Answers1

1

$featured seems to be an empty array so you should first check if it contains elements:

if(count($featured) > 0 && $featured[0]=="Featured")
David Rojo
  • 2,337
  • 1
  • 22
  • 44