Hello, I need help with displaying all images from a post please. I’ve been through code that posts every image ever uploaded but need only all images from a post. The function below can only show one image.
function images_for_slider() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
foreach($matches as $first_img) {
return $first_img[0];
}
}
The original code stated to pass a loop through the array so all images from the same post can be displayed, here’s the original:
function first_image_of_postContent() {
global $post, $posts;
$first_img = ‘’;
$imgString;
ob_start();
ob_end_clean();
$output = preg_match_all(’/<img.+src=’"[’"].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = content_url()."/uploads/default.jpg";
}
$imgString ="<a href=’".get_the_permalink()."’ class=‘thumbnail-wrapper’><img src=’".$first_img."’ alt=’’ class=‘post-excerpt-image’/></a>";
return $imgString;
}