最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

PHP Warning: Invalid argument supplied for foreach() in homepadtvropublic_htmlwp-contentthemesclaudiacontent.php on line 30

programmeradmin5浏览0评论

In the cpanel error log WordPress generates this error:

PHP Warning: Invalid argument supplied for foreach () in /home/padtvro/public_html/wp-content/themes/claudia/content.php on line 30

If I delete it, it appears again in 6-7 hours. What's the problem?

<section id="contents" class="clearfix"><div class="row main_content"><div class="container content_wraper"><?php the_breadcrumb(); ?><div class="grid_8" id="content"><div class="widget_container content_page"><?php require_once dirname(__FILE__) . '/include/cat.php'; ?> 
</div></div><div class="grid_4 p7ehc-a" id="sidebar">
<?php $ge_sidebar = '';
                 if (is_search()) {
                      $ge_sidebar = of_get_option('se_sidebar','');
                    }else if(is_category() ) {

                        $category = get_the_category();                     

                        $cn_sidebar ='';
                        foreach($category as $ca_id) {
                            if(empty($cn_sidebar)) { $cn_sidebar = of_get_option('cat_'.$ca_id->term_id);}                              

                        }

                        if(empty($cn_sidebar)) {
                            $ge_sidebar = of_get_option('cat_sidebar','');
                        } else { $ge_sidebar = $cn_sidebar; }


                    } else if(is_author() ) {

                        $ge_sidebar = of_get_option('au_sidebar','');

                    }else if(is_tag() ) {

                        $tags = get_the_tags();                     

                        $cn_sidebar ='';
                        foreach($tags as $tg_id) {
                            if(empty($cn_sidebar)) { $cn_sidebar = of_get_option('tag_'.$tg_id->term_id);}                              
                        }

                        if(empty($cn_sidebar)) {
                            $ge_sidebar = of_get_option('tag_sidebar','');
                        } else { $ge_sidebar = $cn_sidebar; }
                    }               



                $dyn_sidebar ='';
                if(!empty($ge_sidebar)) {   $dyn_sidebar = $ge_sidebar; };              

                foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {
                    if($sidebar['name'] == $dyn_sidebar)
                        {
                             $dyn_sidebar = $sidebar['id'];
                        }
                } 

                if(!empty($dyn_sidebar)) {
                    if (is_active_sidebar($dyn_sidebar)) : dynamic_sidebar($dyn_sidebar);
                    endif;  
                } else{
                    if (is_active_sidebar('center-sidebar')) : dynamic_sidebar('center-sidebar');
                    endif;
                }

    ?></div><div class="clear"></div></div></div></section>

In the cpanel error log WordPress generates this error:

PHP Warning: Invalid argument supplied for foreach () in /home/padtvro/public_html/wp-content/themes/claudia/content.php on line 30

If I delete it, it appears again in 6-7 hours. What's the problem?

<section id="contents" class="clearfix"><div class="row main_content"><div class="container content_wraper"><?php the_breadcrumb(); ?><div class="grid_8" id="content"><div class="widget_container content_page"><?php require_once dirname(__FILE__) . '/include/cat.php'; ?> 
</div></div><div class="grid_4 p7ehc-a" id="sidebar">
<?php $ge_sidebar = '';
                 if (is_search()) {
                      $ge_sidebar = of_get_option('se_sidebar','');
                    }else if(is_category() ) {

                        $category = get_the_category();                     

                        $cn_sidebar ='';
                        foreach($category as $ca_id) {
                            if(empty($cn_sidebar)) { $cn_sidebar = of_get_option('cat_'.$ca_id->term_id);}                              

                        }

                        if(empty($cn_sidebar)) {
                            $ge_sidebar = of_get_option('cat_sidebar','');
                        } else { $ge_sidebar = $cn_sidebar; }


                    } else if(is_author() ) {

                        $ge_sidebar = of_get_option('au_sidebar','');

                    }else if(is_tag() ) {

                        $tags = get_the_tags();                     

                        $cn_sidebar ='';
                        foreach($tags as $tg_id) {
                            if(empty($cn_sidebar)) { $cn_sidebar = of_get_option('tag_'.$tg_id->term_id);}                              
                        }

                        if(empty($cn_sidebar)) {
                            $ge_sidebar = of_get_option('tag_sidebar','');
                        } else { $ge_sidebar = $cn_sidebar; }
                    }               



                $dyn_sidebar ='';
                if(!empty($ge_sidebar)) {   $dyn_sidebar = $ge_sidebar; };              

                foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {
                    if($sidebar['name'] == $dyn_sidebar)
                        {
                             $dyn_sidebar = $sidebar['id'];
                        }
                } 

                if(!empty($dyn_sidebar)) {
                    if (is_active_sidebar($dyn_sidebar)) : dynamic_sidebar($dyn_sidebar);
                    endif;  
                } else{
                    if (is_active_sidebar('center-sidebar')) : dynamic_sidebar('center-sidebar');
                    endif;
                }

    ?></div><div class="clear"></div></div></div></section>

Share Improve this question edited Mar 31, 2020 at 15:46 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Mar 31, 2020 at 12:55 Pop DanielPop Daniel 112 bronze badges 1
  • 2 Is this from a theme you developed, or one you bought/downloaded. If it's from another developer you need to raise the issue with them so that they can resolve the issue in a way that will be safe from updates. – Jacob Peattie Commented Mar 31, 2020 at 13:10
Add a comment  | 

1 Answer 1

Reset to default 1

The error is generated by this code:

line 27 $tags = get_the_tags();                     
line 28
line 29                    $cn_sidebar ='';
line 30                    foreach($tags as $tg_id) {

If a post has no tags, get_the_tags() wil return false. Then supplying false to the foreach statement will get you the error mentioned. In order to prevent this you should wrap the foreach in if (!empty($tags)) { foreach ...}.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论