Enjoy an ad free experience by logging in. Not a member yet?
Register .
04-26-2012, 12:32 AM
PM User |
#1
New to the CF scene
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Warning: Invalid argument supplied for foreach()
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
PHP Code:
<?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_size ( 120 , 120 , true ); // 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_id , implode ( ',' , $add_tags ));
}
?>
04-26-2012, 01:15 AM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
The foreach item simply isn't iterable. The second error will disappear when you fix the first one.
The error I can fix, but the cause is unknown. You'll need to determine why you have no valid array or traversable type to iterate. The best I can suggest is to mask it.
PHP Code:
$terms = get_the_terms ( $post_id , 'post_tag' ); if ( is_array ( $terms ) || ( is_object ( $terms ) && ( $terms instanceof Traversable ))) { foreach ( $terms AS $term ) ... }
I'd strongly recommend against this approach:
PHP Code:
foreach( get_the_terms ( $post_id , 'post_tag' ) as $term ) if(! in_array ( $term -> slug , $tags_to_add )) $add_tags [] = $term -> slug ;
Doing so is a visual discrepancy since its no longer easy to determine what condition $add_tags assignment will occur. Brace these up for simple clarity.
04-26-2012, 01:54 AM
PM User |
#3
New to the CF scene
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
When i update i get this error
PHP Code:
$terms = get_the_terms ( $post_id , 'post_tag' ); if ( is_array ( $terms ) || ( is_object ( $terms ) && ( $terms instanceof Traversable ))) { foreach ( $terms AS $term ) ... }
Parse error: syntax error, unexpected '.' in /home/content/46/8529846/html/wp-content/themes/super-light/functions.php on line 98
04-26-2012, 03:11 AM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
. . .
I assume you replace . . . in there with your own code?
04-26-2012, 03:21 AM
PM User |
#5
New to the CF scene
Join Date: Apr 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Fou-Lu
. . .
I assume you replace . . . in there with your own code?
I did and i got the error fixed but the code won't work. Ultimately i have custom fields in wordpress and i would like to convert them to tags. I'm not sure what i am doing wrong. I added this to my functions
PHP Code:
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( $tag1 , $tag2 , $tag3 , $tag4 , $tag5 , $tag6 , $tag7 , $tag8 , $tag9 );
if ( $terms = get_the_terms ( $post_id , 'post_tag' )) {
$add_tags = array( $tag1 , $tag2 , $tag3 , $tag4 , $tag5 , $tag6 , $tag7 , $tag8 , $tag9 );
$terms = get_the_terms ( $post_id , 'post_tag' );
if ( is_array ( $terms ) || ( is_object ( $terms ) && ( $terms instanceof Traversable )))
{
foreach ( $terms AS $term )
if (! in_array ( $term -> slug , $tags_to_add )) {
$add_tags [] = $term -> slug ;
}
}
if (!empty( $add_tags )) {
wp_add_post_tags ( $post_id , implode ( ',' , $add_tags ));
}
}
}
04-26-2012, 05:05 AM
PM User |
#6
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
I've already mentioned that this won't show any content. The error is now taken care of, but you need to determine why you have no valid array or traversable object.
You need to find out why get_the_terms() function doesn't return a traversable type.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 04:14 AM .
Advertisement
Log in to turn off these ads.