Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKristian Woods
23,414 PointsWhy am I getting the error: PHP Warning: Illegal string offset 'url' with Advanced Custom Fields?
I'm using Advanced Custom Fields, and I'm having issues displaying my masthead images. I have a Repeater Field that contains one sub-row "mast-head-images" - which is just an Image field type. In my field settings, I'm pulling the image array, and using that array to grab the URL in my markup. However, I keep getting an error " Illegal string offset 'url'"
I used "print_r" and "echo" to display the value returned, and the correct image URL is being returned. However, the images don't display.
I've made sure that I'm up-to-date and using the latest version of AFC. I don't know if I should change my PHP version? (currently 7.2.1 - I tried reverting back to an older version, but nothing changed.)
I've also deactivated other plugins to test that its not a conflict of plugins - but still nothing.
I've actually hard coded the URLs to rule out anything with the path, and it works perfectly. - my images display. I just need to get it working with AFC, instead.
<header>
<?php ?>
<?php
$defaults = array (
'theme_location' => 'main-navigation',
'menu_class' => 'no-bullet',
'container' => false
); ?>
<nav class="main-header-nav">
<img src="<?php echo get_template_directory_uri();?>/icons/Hilton-logo.svg" alt="Hilton Logo" class="header-logo">
<?php wp_nav_menu($defaults); ?>
</nav>
<?php
$topMastHeader = get_field('top_header');
$bottomMastHeader = get_field('bottom_header');
?>
<h3 class="header-text tagline">
<?php echo $topMastHeader; ?>
</h3>
<h1 class="header-text">
<?php echo $bottomMastHeader; ?>
</h1>
<div class="cycle-slideshow carousel-container" data-cycle-fx="scrollHorz" data-cycle-timeout="0" data-cycle-prev="#prev" data-cycle-next="#next" data-cycle-caption=".carousel-counter" data-cycle-caption-template="<span class='bigger'>{{slideNum}}/ </span> <span class='smaller'>{{slideCount}}</span>">
<div class="cycle-caption"></div>
<?php while( have_rows('header_carousel') ): the_row();
// vars
$mastHeadImages = get_sub_field('mast_head_images');
?>
<?php $i = 0;
foreach( $mastHeadImages as $image ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" class="carousel-container__img carousel-img-<?php echo $i; ?>" />
<?php $i++; endforeach; ?>
<!-- IMAGE URL HARD CODED -->
<!--
<img src="http://localhost:8888/wordpress/wp-content/uploads/2018/07/premium-king-room-1900x1060-1345x750.jpg" alt="">
<img src="http://localhost:8888/wordpress/wp-content/uploads/2018/07/woman-at-canopy-central-cafe-1900x1060-1-1345x750.jpg" alt="">
<img src="http://localhost:8888/wordpress/wp-content/uploads/2018/07/hotel-exterior-1900x1060-1-1345x750.jpg" alt="">
-->
<div class="masthead_overlay"></div>
</div>
<div class="center carousel-control-container">
<a href="#" id="prev">
<span class="icon-arrow-left"></span>
</a>
<a href="#" id="next">
<span class="icon-arrow-right"></span>
</a>
<div class="carousel-counter">
</div>
</div>
<?php endwhile; ?>
</header>
2 Answers
Rama Olson
11,670 PointsDo you have your ACF Image field set to return an array or a url? Check that it is set to return an array and not a url as the url returns a string and would give you that error.
Kristian Woods
23,414 PointsLike, I mentioned - I’m pulling the image array. I’ve also tried pulling the ID and URL. Still nothing
Rama Olson
11,670 PointsAh I missed that, sorry. Sounds like a tricky one. Is your repeater field 'header_carousel'?
If that's the case and the image field in your repeater is 'mast_head_images', I don't think you need a separate foreach loop on that do you?
If you're already looping through the repeater with while( have_rows('header_carousel') ): the_row();
Then I think you'd just get the image field like you're doing: $mastHeadImages = get_sub_field('mast_head_images');
And then just display it: <img src="<?php echo $mastHeadImages['url']; ?>">
Since you're already in the while loop for your repeater you don't need another for each loop do you?
Unless the mast_head_images field is also a repeater, but then I think you'd need to do another while( have_rows('mast_head_images') ) function on that and then get the image sub field inside that repeater.
Maybe try just displaying the image sub field without that extra foreach loop, unless I'm missing something.