robertsmith17 Posted March 26, 2013 Share Posted March 26, 2013 (edited) I'm a complete newbie with PHP - have been making some changes on a WordPress site and ran across an error "Warning: Invalid argument supplied for foreach() in /home/thesite/public_html/wp-content/themes/site1/custompost_entertainment.php on line 35" if ($post->post_type == 'page'){ echo apply_filters('the_content',$post->post_content); echo "<section id='featured_pages'>"; foreach ($customPosts as $date=>$posts){ $date = new DateTime($date); foreach ($posts as $page){ $custom_meta = get_post_custom($page->ID); Any ideas? Edited March 26, 2013 by robertsmith17 Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/ Share on other sites More sharing options...
akphidelt2007 Posted March 26, 2013 Share Posted March 26, 2013 (edited) It means $customPosts or $posts is not an array. You have to provide an array even if it's an empty array. If the array is not explicitly set, than before you run the foreach statement... do an if(isset($customPosts)) { foreach() } Edited March 26, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421167 Share on other sites More sharing options...
shlumph Posted March 26, 2013 Share Posted March 26, 2013 Not to mention $date=>$posts is not valid syntax. Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421169 Share on other sites More sharing options...
Barand Posted March 26, 2013 Share Posted March 26, 2013 Not to mention $date=>$posts is not valid syntax. it is if $posts contains the name of an object property $arr = array ('a' => 100, 'b' => 200); $obj = (object)$arr; $x = 'a'; echo $obj->$x; // 100 $x = 'b'; echo $obj->$x; // 200 Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421184 Share on other sites More sharing options...
shlumph Posted March 26, 2013 Share Posted March 26, 2013 it is if $posts contains the name of an object property $arr = array ('a' => 100, 'b' => 200); $obj = (object)$arr; $x = 'a'; echo $obj->$x; // 100 $x = 'b'; echo $obj->$x; // 200 Ahh, you are right. I retract that statement, for some reason I read it as $date=>posts. Like OP was trying to access an object variable. Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421220 Share on other sites More sharing options...
robertsmith17 Posted March 26, 2013 Author Share Posted March 26, 2013 Thanks for the replies, I really appreciate it. I don't really understand the syntax of foreach ($customPosts as $date=>$posts){ Like I said - I'm a complete novice with PHP. I'm a lot more comfortable with the likes of C#, where a foreach loop would be written something like this (for example): foreach (String str in stringArray){ I'm afraid I just don't understand "$customPosts as $date=>$posts" at all. Could someone pseudo-code it for me? Or explain it in terms of C#/Java/JS type syntax? Thanks again. Sorry for being a bit dense... Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421255 Share on other sites More sharing options...
jcbones Posted March 26, 2013 Share Posted March 26, 2013 Well, try looking at it like: foreach($array as $index => $value) { Quote Link to comment https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/#findComment-1421261 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.