• Resolved Paradise

    (@birobas)


    Warning: Invalid argument supplied for foreach() in C:\wamp64\www\wordpress\wp-content\themes\uniao-site\index.php on line 42

    <?php 
    
    add_filter( 'rwmb_media_add_string', 'prefix_change_add_string' );
    function prefix_change_add_string() {
        return '+ Adicionar imagens';
    }
    
    add_filter( 'rwmb_meta_boxes', 'meta_box_slide' );
    function meta_box_slide( $meta_boxes ) {
        $meta_boxes[] = array(
            'title'  => 'Imagens do Slide',
            'post_types' => 'slide',
            'fields' => array(
                array(
                    'id'               => 'image',
                    'name'             => 'Imagens',
                    'type'             => 'image_advanced',
                
                    // Delete image from Media Library when remove it from post meta?
                    // Note: it might affect other posts if you use same image for multiple posts
                    'force_delete'     => false,
                
                    // Maximum image uploads.
                    'max_file_uploads' => 4,
                
                    // Do not show how many images uploaded/remaining.
                    'max_status'       => 'false',
                
                    // Image size that displays in the edit page.
                    'image_size'       => 'thumbnail',
                ),
                
            ),
        );
        return $meta_boxes;
    }
    
    
    <?php
    		$args = array(
    			'post_type' => 'slide'
    		);
    		
    		$query = new WP_Query( $args );
    		if ( $query->have_posts() ) {
    			echo '<ul class="category posts">';
     
            	// Start looping over the query results.
           		while ( $query->have_posts() ) {
     
                $query->the_post();
     
                ?>
     
                <li <?php post_class( 'left' ); ?>>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php the_title(); ?>
    				</a>
    				<?php 
    				$images = rwmb_meta( 'info', array( 'size' => 'thumbnail' ) );
    foreach ( $images as $image ) {
        echo '<a href="', $image['full_url'], '"><img src="', $image['url'], '"></a>';
    }
    ?>
                </li>
     
                <?php
         
            }
     
        echo '</ul>';
     
    }
     
    // Restore original post data.
    wp_reset_postdata();
     
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi Randys,

    The warning comes from posts which don’t have images uploaded. Please change your code to:

    $images = rwmb_meta( 'info', array( 'size' => 'thumbnail' ) );
    if ( ! empty( $images ) ) {
        foreach ( $images as $image ) {
            echo '<a href="', $image['full_url'], '"><img src="', $image['url'], '"></a>';
        }
    }
    Thread Starter Paradise

    (@birobas)

    Hi,
    but there are uploaded images in the post

    View post on imgur.com

    View post on imgur.com

    Plugin Author Anh Tran

    (@rilwis)

    As you do query to get multiple posts, if there’s only one post doesn’t have images, then the error still occur. So, please check all posts, or use the check I provided above.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Error: Invalid argument supplied for foreach()’ is closed to new replies.