Author Topic: Warning: Invalid argument supplied for foreach()  (Read 705 times)

hitstopz

  • Just Joined
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Warning: Invalid argument supplied for foreach()
« on: April 25, 2012, 07:05:38 pm »
I keep getting the following error.
Warning: Invalid argument supplied for foreach() in /home/content/46/8529846/html/wp-content/themes/super-light/functions.php on line 94

Warning: Cannot modify header information - headers already sent by (output started at /home/content/46/8529846/html/wp-content/themes/super-light/functions.php:94) in /home/content/46/8529846/html/wp-includes/pluggable.php on line 866


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
   
   
<?php
   
if ( ! isset( $content_width ) )
   
$content_width 650;
   
   
add_action'widgets_init''super_light_sidebars' );
   
   function 
super_light_sidebars() {
   
register_sidebar(array(
      
'name' => __'Sidebar Widget Area''super_light'),
      
'id' => 'sidebar-widget-area',
      
'description' => __'The sidebar widget area''super_light'),
      
'before_widget' => '<div class="widget">',
      
'after_widget' => '</div>',
      
'before_title' => '<h3>',
      
'after_title' => '</h3>',
  ));
  }
  
  
register_nav_menus(
      array(
        
'primary' => __('Header Menu''super_light'),
        
'secondary' => __('Footer Menu''super_light')
      )
  );
  
  
//Multi-level pages menu
  
function super_light_page_menu() {
  
  if (
is_page()) { $highlight "page_item"; } else {$highlight "menu-item current-menu-item"; }
  echo 
'<ul class="menu">';
  
wp_list_pages('sort_column=menu_order&title_li=&link_before=&link_after=&depth=3');
  echo 
'</ul>';
  }
  
  
//Single-level pages menu
  
function super_light_menu_flat() {
      if (
is_page()) { $highlight "page_item"; } else {$highlight "menu-item current-menu-item"; }
      echo 
'<ul class="menu">';
      
wp_list_pages('sort_column=menu_order&title_li=&link_before=&link_after=&depth=1');
      echo 
'</ul>';
  }
  
  
  
  
  
add_editor_style();
  
add_theme_support('automatic-feed-links');
  
add_theme_support('post-thumbnails');
  
  
set_post_thumbnail_size120120true ); // Default size
  
  // Make theme available for translation
  // Translations can be filed in the /languages/ directory
  
load_theme_textdomain('super_light'get_template_directory() . '/languages');
  
  function 
catch_that_image() {
    global 
$post$posts;
    
$first_img '';
    
ob_start();
    
ob_end_clean();
    
$output preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i'$post->post_content$matches);
    
$first_img $matches [1] [0];
  
    if(empty(
$first_img)){ //Defines a default image
      
$first_img "/images/default.jpg";
    }
    return 
$first_img;
  }
  
  
add_action('save_post','custom_field_add_tags');
  
  function 
custom_field_add_tags($post_id) {
  
   
$post get_post($post_id);
  
   
//get values of custom fields and put into array
  
   
$tag1 get_post_meta($post_id'tag_name1'true);
   
$tag2 get_post_meta($post_id'tag_name2'true);
   
$tag3 get_post_meta($post_id'tag_name3'true);
   
$tag4 get_post_meta($post_id'tag_name4'true);
   
$tag5 get_post_meta($post_id'tag_name5'true);
   
$tag6 get_post_meta($post_id'tag_name6'true);
   
$tag7 get_post_meta($post_id'tag_name7'true);
   
$tag8 get_post_meta($post_id'tag_name8'true);
   
$tag9 get_post_meta($post_id'tag_name9'true);
  
   
$tags_to_add = array($tag1$tag2$tag3$tag4$tag5$tag6$tag7$tag8$tag9);
  
   
//now check if tag does not already exist (if no - add tag from custom field)
  
   
$add_tags = array();
  
   foreach(
get_the_terms($post_id'post_tag') as $term)
  
      if(!
in_array($term->slug$tags_to_add))
          
$add_tags[] = $term->slug;
  
  if(!empty(
$add_tags))
     
wp_add_post_tags($post_idimplode(','$add_tags));
 }
 
 
?>
 

 


PHP Help Forum

Warning: Invalid argument supplied for foreach()
« on: April 25, 2012, 07:05:38 pm »


Ojoshiro

  • Technocentror
  • Senior Member
  • ****
  • Posts: 150
  • Karma: 4
  • That is not dead which can eternal loop...
    • View Profile
Re: Warning: Invalid argument supplied for foreach()
« Reply #1 on: April 27, 2012, 04:01:48 am »
In the code you posted I can only find one 'foreach',
Code: [Select]
1
 foreach(get_the_terms($post_id, 'post_tag') as $term)
. If that's the one throwing the error, look at what get_the_terms() returns.
T.A.N.S.T.A.A.F.L.
_______________________________/

Sarthak Patel

  • Senior Member
  • ****
  • Posts: 368
  • Karma: 6
    • View Profile
    • HOSD
Re: Warning: Invalid argument supplied for foreach()
« Reply #2 on: April 27, 2012, 06:03:24 am »
hello hitstopz,

 i think value return by get_the_terms($post_id, 'post_tag') is not array that's why it's showing error.
value return from get_the_terms($post_id, 'post_tag') must be array because you can only pass array to foreach.
check this.
http://php.net/manual/en/control-structures.foreach.php

~~SR~~