【问题标题】:Please assist me in resolving an Undefined offset: 0 in /public_html/wp-content/themes/modus-child/functions.php on line 51 [duplicate]请帮助我解决 Undefined offset: 0 in /public_html/wp-content/themes/modus-child/functions.php 第 51 行 [重复]
【发布时间】:2021-06-06 11:15:13
【问题描述】:

在与原始 Web 开发人员的沟通中断后,我继承了一个有问题的网站。

我一直在努力解决并解决尽可能多的错误。

PHP 注意:未定义的偏移量:0 in /home/customer/www/public_html/wp-content/themes/modus-child/functions.php 第 51 行

长期目标是使用不同的主题重新构建网站,但我希望能在解决此错误方面提供一些帮助,以便在开发过程中为我们提供安全网。

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

从第 46 行开始 第 51 行是以 if($featured[0]=="Featured"){ 开头的行 我可以在括号中看到偏移量 0。

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

【问题讨论】:

  • 该错误意味着$featured 在索引/键0 下不包含任何元素。我们不知道脚本试图在那里读取什么样的值,所以我们不知道get_field应该返回到这里。如果一开始没有为该字段存储任何值,它可能只是简单地返回 null ——在这种情况下,如果要跳过任何进一步的处理,那么用 if($featured &amp;&amp; $featured[0]=="Featured") 替换该行已经可以解决问题了。
  • 不是 Wordpress 人,但来自手册 get_field() 返回标量值而不是数组

标签: php html undefined offset


【解决方案1】:

$featured 似乎是一个空数组,所以你应该首先检查它是否包含元素:

if(count($featured) > 0 && $featured[0]=="Featured")

【讨论】:

  • 这个答案不需要改进。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-01-12
  • 2023-01-31
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2022-12-17
  • 2014-11-09
  • 2020-12-29
相关资源
最近更新 更多