I’ve created a Custom Post Type, “Slide”, and I’m attempting to load a random slide image, with title and caption, in my sidebar. I’m using the Advanced Custom Fields Plugin for the post/slide creation. The code below is what I have in my sidebar.php – I do get a random post loaded in the sidebar (showing title and caption) but the image URL does not output/echo, just <img src="" />
Can anyone tell if I have ACF code wrong or maybe using the WP_Query wrong?
<?php $args = array(
'post_type' => 'slide',
'posts_per_page' => 1,
'orderby' => rand
);
$attachment_id = get_field('slide_photo');
$size = "medium"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
// url = $image[0];
// width = $image[1];
// height = $image[2];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<img src="';
echo $image[0];
echo '" />';
the_title();
the_field('slide_credit');
endwhile; ?>
var_dump ($attachment_id);
outputs: string(58) "path- to-image". 58 being the post id and path-to-image is the full path on my site.var_dump ($image);
outputs: bool(false)