The below code works as intended (displays the first row of my repeater field) but if I delete all the repeater entries, I receive this error message:
Warning: Illegal string offset ‘slides_slide-image’ in [PATH TO FILE] on *line 12
*the line that starts with: $first_row_image
Can anyone shed some light for me?
<?php $args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 21
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$rows = get_field('slides' );
$first_row = $rows[0];
$first_row_image = $first_row['slides_slide-image' ];
$image = wp_get_attachment_image_src( $first_row_image, 'circle' );
?>
<?php if ($rows) { ?>
<div class="subdivision">
<?php if ($rows) { ?>
<a href="<?php the_permalink();?>"><img src="<?php echo $image[0]; ?>" /></a>
<?php } else { ?>
<a href="<?php the_permalink();?>"><img src="<?php bloginfo('template_directory'); ?>/_img/photo-coming-soon.png" /></a>
<?php } ?>
<h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
</div><!-- .subdivision -->
<?php endwhile; ?>
You need to do a check to make sure that your operation returned a value. For example
`
$rows = get_field(‘slides’ );
if (is_array($rows) && count($rows)) {
$first_row = $rows[0];
$first_row_image = $first_row[‘slides_slide-image’ ];
$image = wp_get_attachment_image_src( $first_row_image, ‘circle’ );
}
Right on! Thank you. I would not have figured that out 🙁 Looks to be working with your code. I really appreciate it!