I am trying to output a bunch of images after each post but I can only output one.

add_filter( 'the_content', 'image_posts' );
function image_posts( $content ) {
    global $post;
    if ( ! $post instanceof WP_Post ) {
        return $content;
    }
    $all_images = get_images(); // array of images 
    switch ( $post->post_type ) {
        case 'page':
            // return $content; // this only return content
            foreach ($all_images as $image){
                echo '<img src="'.$image.'" />';
            }
            // return $content; // this returns images on top of the content


        default:
            return $content;
    }
}

and if I modify the loop to this:

foreach ($all_images as $image){
   return content . '<img src="'.$image.'" />';
}

I only get the content with one image. How can I output all the images after the content?

Read more here: Return array of images after content


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: