Search code examples
arrayswordpresswhile-loopadvanced-custom-fields

Advice on Error: Array to string conversion. Wordpress


I have looked for a solution to this error. I would just like some advice as I think it's something small that I'm missing.

I'm trying to get a url field and image field, using flexible content, from an options page set up with ACF pro. I'm getting the data, but not as a string variable.

Here's a screenshot of the error. NEW LINK http://redrocketwebsitedesign.co.uk/Array_error2.png

And the code that generates this.

    <ul class="list-inline"><li>
    <p>Follow us on</p>
  </li>
  <?php //Options: Social icons

// check if the flexible content field has rows of data
if( have_rows('social_icon', 'option') ):

     // loop through the rows of data
    while ( have_rows('social_icon', 'option') ) : the_row();

        if( get_row_layout() == 'social_icon' ):


$image = get_sub_field_object('social_image', 'option');

if( $image ){
?>


<li>
<a href="<?php echo the_sub_field('social_link', 'option'); ?>" target="_blank">
<img class="img img-responsive" src="<?php echo $image[7]; ?>" alt="<?php echo $image[alt]; ?>"/>
</a> 
<?php var_dump('Vardump <br />') ?>
<?php var_dump($url) ?>
<?php var_dump('<br /> Image array <br />') ?>
<?php var_dump($image) ?>
<?php var_dump('<br /> Image link <br />') ?>
<?php var_dump($image['7']) ?>
</li>

<?php 
}

endif;

    endwhile;
    else :

    // no layouts found

endif;

  ?>


</ul>

Any help would be greatly appreciated :-]

$social_image is the $field_name, and is stored as an image array. When I print_r $image, this is the result.I think I'm close to being able to pick the right string from the array.

    Image array

`" array(22) { ["ID"]=> int(138) ["key"]=> string(19) "field_54e47a9b7f735" ["label"]=> string(12) "Social Image" ["name"]=> string(12) "social_image" ["prefix"]=> string(0) "" ["type"]=> string(5) "image" ["value"]=> array(18) { ["ID"]=> int(105) ["id"]=> int(105) ["title"]=> string(11) "Tripadvisor" ["filename"]=> string(24) "footertripadvisorNEW.png" ["url"]=> string(83) "http-link" ["alt"]=> string(11) "Tripadvisor" ["author"]=> string(1) "1" ["description"]=> string(0) "" ["caption"]=> string(0) "" ["name"]=> string(20) "footertripadvisornew" ["date"]=> string(19) "2015-02-10 16:13:27" ["modified"]=> string(19) "2015-02-18 11:52:15" ["mime_type"]=> string(9) "image/png" ["type"]=> string(5) "image" ["icon"]=> string(68) "http://localhost:8888/wordpress/wp-includes/images/media/default.png" ["width"]=> int(50) ["height"]=> int(50) ["sizes"]=>`

..etc.


Solution

  • get_sub_field() takes a single argument of type string. You're currently passing in too many. You should be using:

    $value = get_sub_field( $field_name );
    

    Also, as mentioned, you need to ensure that $field_name is actually a string, and not an array.