Support » Fixing WordPress » Need help resolving Warning: Invalid argument supplied for foreach() in /home/te

  • The complete error is:

    Warning: Invalid argument supplied for foreach() in /home/telapak5/public_html/wp-content/themes/smartstart/functions/custom-functions.php on line 337

    It’s occurring on my homepage : http://www.telapakkarya.com

    lines 335
    $meta = wp_get_attachment_metadata( $id );
    lines 337
    foreach( $meta[‘sizes’] as $size => $values ) {
    lines 339
    if( $values[‘file’] == $file && $url == array_shift( wp_get_attachment_image_src( $id, $size ) ) ) {
    lines 340
    //$this->attachment_size = $size;
    lines 341
    return $id;

    If anyone know how to fix this error, I’d greatly appreciate learning how to fix it. Thanks everyone!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    You can wrap the foreach loop with an if statement that checks whether condition is true before executing the loop.
    E.g

    if ( $meta['sizes'] ) {
     foreach ( $meta['sizes'] as $size => $values ) {
      ...
     }
    }

    Its is possible that their isn’t anything returned from the wp_get_attachment_metadata() function or $meta[‘sizes’] is not exists or its not an array.

    Try this:

    $meta = wp_get_attachment_metadata( $id );
    
    if($meta && isset($meta['sizes'])){ // check for if we have anything in $meta['size']
    foreach( $meta['sizes'] as $size => $values ) {
        if( $values['file'] == $file && $url == array_shift(wp_get_attachment_image_src( $id, $size ) ) ) {
        //$this->attachment_size = $size;
        return $id;
        } // if ends
    } // foreach ends
    }

    after I edit a message like this

    Parse error: syntax error, unexpected $end in /home/telapak5/public_html/wp-content/themes/smartstart/functions/custom-functions.php on line 1359

    line 1359
    add_action(‘ss_framework_custom_scripts’, ‘ss_framework_insert_sliders_scripts’);

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Make sure you’ve closed off the if statement’s parenthesis.

    This now appears

    Parse error: syntax error, unexpected T_IF in /home/telapak5/public_html/wp-content/themes/smartstart/functions/custom-functions.php on line 1359

    line 1359
    add_action if(‘ss_framework_custom_scripts’, ‘ss_framework_insert_sliders_scripts’);

    Try using a code editor to edit the file. Most of the code editor will tell you if you have a syntax error. What editor are you using?

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    What’s the line before the if statement and can you post your if statement?

    the line before the if statement

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser. ]

    if ( !function_exists('ss_framework_insert_sliders') ) {
    
    	function ss_framework_insert_sliders_scripts() {
    
    		global $wpdb;
    
    		$query = "SELECT post_id
    				  FROM $wpdb->postmeta
    				  WHERE meta_key = 'ss_slider_slides'";
    
    		$sliders = $wpdb->get_results( $query );
    
    		foreach ( $sliders as $slider ):
    
    			$post_obj = get_post( $slider->post_id );
    
    			// Check that slider actually exist, so we don't insert unnecessary code
    			if( !ss_framework_has_shortcode('slider') && get_post_type() != 'slider' )
    				continue;
    
    		?>
    
    <script>
    
    (function( $ ) {
    
    	var $slider = $('#<?php echo $post_obj->post_name; ?>');
    
    	if( $slider.length ) {
    
    		// Prevent multiple initialization
    		if( $slider.data('init') === true )
    			return false;
    
    		$slider.data( 'init', true )
    			   .smartStartSlider({
    				   width              : <?php echo ss_framework_get_custom_field('ss_slider_width', $slider->post_id ); ?>,
    				   height             : <?php echo ss_framework_get_custom_field('ss_slider_height', $slider->post_id ); ?>,
    				   contentSpeed       : <?php echo ss_framework_get_custom_field('ss_slider_content_speed', $slider->post_id ); ?>,
    				   showContentOnhover : <?php echo ss_framework_get_custom_field('ss_slider_show_content_onhover', $slider->post_id ); ?>,
    				   hideContent        : <?php echo ss_framework_get_custom_field('ss_slider_hide_content', $slider->post_id ); ?>,
    				   contentPosition    : "<?php echo ss_framework_get_custom_field('ss_slider_content_position', $slider->post_id ); ?>",
    				   timeout            : <?php echo ss_framework_get_custom_field('ss_slider_autoplay', $slider->post_id ); ?>,
    				   pause              : <?php echo ss_framework_get_custom_field('ss_slider_stop_on_click', $slider->post_id ); ?>,
    				   pauseOnHover       : <?php echo ss_framework_get_custom_field('ss_slider_pause_on_hover', $slider->post_id ); ?>,
    				   type               : {
    					   mode           : "<?php echo ss_framework_get_custom_field('ss_slider_transition', $slider->post_id ); ?>",
    					   speed          : <?php echo ss_framework_get_custom_field('ss_slider_speed', $slider->post_id ); ?>,
    					   easing         : "<?php echo ss_framework_get_custom_field('ss_slider_easing', $slider->post_id ); ?>",
    					   seqfactor      : <?php echo ss_framework_get_custom_field('ss_slider_seq_factor', $slider->post_id ); ?>
    				   }
    			   });
    
    		// Detect swipe gestures support
    		if( Modernizr.touch ) {
    
    			function swipeFunc( e, dir ) {
    
    				var $slider = $( e.currentTarget );
    
    				if( dir === 'left' ) {
    					$slider.find('.pagination-container .next').trigger('click');
    				}
    
    				if( dir === 'right' ) {
    					$slider.find('.pagination-container .prev').trigger('click');
    				}
    
    			}
    
    			$slider.swipe({
    				swipeLeft       : swipeFunc,
    				swipeRight      : swipeFunc,
    				allowPageScroll : 'auto'
    			});
    
    		}
    
    	}
    
    })( jQuery );
    
    </script>
    
    		<?php
    
    		endforeach;
    
    	}
    
    }
    add_action if ('ss_framework_custom_scripts', 'ss_framework_insert_sliders_scripts');

    some of my other portfolio such problems do not occur

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Just the one line before the if statement suggested from forum volunteers of this thread and the one line of the if statement, please.

    sorry, I do not understand the point? can be explained

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    I’m trying to find the original the line with your parse error.

    maybe you can visit my website and check out the portfolio section click PPSD 28 it should be next and previous mark like the others.

    http://www.telapakkarya.com

    thank you

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    What is supposed to happen when I visit that link?
    I get a never-ending loading .gif.

    I have a similar problem. =(

    Warning: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values! in /home/leandror/public_html/wp-content/themes/smartstart/functions/custom-functions.php on line 633

    [ 1,777 lines of code moderated, that’s just way too much. For that many lines of code please use pastebin.com instead. ]

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Need help resolving Warning: Invalid argument supplied for foreach() in /home/te’ is closed to new replies.