0

我有这个产生警告的 PHP 代码:

Warning: Invalid argument supplied for foreach() php wordpress

这是代码:

<?php   
    $post_status1  = 'publish'; 
    $post_type1 = 'page';
    $featucat = "about";
    $featucount = "1";

    $my_query = new WP_Query('post_status='. $post_status1 .'&post_type='. $post_type1.'');  
    if ($my_query->have_posts()){
        while ($my_query->have_posts()) : $my_query->the_post();

            $front_values = get_post_custom_values('Homepage_Blog_01p', get_the_ID());
            foreach ( $front_values as $front_key => $result_value ) {
                if($result_value == 'about') {
?>

                    <div class="thewidgets">
                        <?php
                        $description_values = get_post_custom_values('Description_Field', get_the_ID());
                        foreach ( $description_values as $description_key => $description_value ) {
                            echo $description_value;

                        }
                        ?>
                        <a href="<?php the_permalink(); ?>" title="Read the whole post" class="rm">Read More</a>
                    </div>
    <?php } } endwhile; } ?>

这是完整的错误:

Warning: Invalid argument supplied for foreach() in 
D:\PROGRAM FILES\wamp\www\westchester\wp-content\themes\
computerrepair\footer.php on line 22"

我究竟做错了什么?

4

3 回答 3

1

$front_values如果你得到它,它不是一个数组。检查它的内容,如果它有时不是一个合法的数组(例如,如果在没有任何结果时get_post_custom_values返回),则通过将其包装在一个条件中来解决它。nullforeachif(is_array($front_values)) {

于 2013-08-18T20:02:44.420 回答
0

如何重现此 PHP 警告:

把这个放在a.php

<?php
$skipper = "abcd";
foreach ($skipper as $item){       //warning happens on this line.
    print "ok";
}

?>

印刷:

eric@dev ~ $ php a.php
PHP Warning:  Invalid argument supplied for foreach() in 
/var/www/sandbox/eric/code/php/run06/a.php on line 3
PHP Stack trace:

警告的意思正是它所说的。您将一个参数传递到 foreach 结构中,该参数无法在 foreach 中进行评估。在foreach循环之前,确保第一个参数是foreach可以处理的结构。

于 2014-08-11T18:03:13.140 回答
0
<?php wp_head(); ?>

Add this code in the head tag. I think it will solve your warning - Warning: Invalid argument supplied for foreach() in C:\wamp64\www\development\wp-includes\script-loader.php on line 2652

于 2021-07-26T16:15:46.580 回答