警告:为foreach()php wordpress提供的参数无效

时间:2013-08-18 20:00:46

标签: php wordpress

我有这个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"

我做错了什么?

3 个答案:

答案 0 :(得分:1)

$front_values如果您获得该数据,则不是数组。检查其内容,如果它有时合法地不是数组(例如,如果get_post_custom_values在没有任何结果时返回null),请通过包装来解释它foreach有条件的if(is_array($front_values)) {

答案 1 :(得分: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可以处理的结构。

答案 2 :(得分:0)

<?php wp_head(); ?>

在 head 标签中添加此代码。我认为它会解决您的警告 - 警告:为 C:\wamp64\www\development\wp-includes\script-loader.php 中的 foreach() 提供的参数无效,第 2652 行