Jump to content

Recommended Posts

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 by robertsmith17
Link to comment
https://forums.phpfreaks.com/topic/276178-simple-help-for-broken-php/
Share on other sites

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 by akphidelt2007

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

 

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.

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.