WordPress Help – multiple images per page/post in the sidebar

I am having a little trouble with wordpress, the admin needs to be able to post up to 5 images per page/post and then I need to be able spit those images out into the template.

Is there a plugin or something similar that will provide me with this functionality? I would be most grateful if any one could offer some advice, I have been able to find anything by googling.

Related posts

Leave a Reply

2 comments

  1. This line should do the trick:

    $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image') );
    

    if you need it for outside the loop then I would turn it into a function and add it to your functions.php page.

  2. WordPress keeps uploaded images as attachments, which are really just child posts of post_type ‘attachment’ in the wp_posts table.

    Upload your images on the page/post/custom post type in question (The upload icon above the editor) and a ‘Gallery’ is created.

    To display the photos you can make a widget and sidebar (see here and here) or display them directly in the loop of your choice with the code below. This is essentially the same as using the [gallery] shortcode directly in the post content.

    <?php 
        //Gather the child posts (attachments) of mime type 'image'
        $photos = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image') );
    
        //If there are any attachments..
        if (!empty($photos)) :
            //Loop through each attachment..
            foreach ($photos as $photo_id => $photo) : 
                //And render the <img> tag
                echo wp_get_attachment_image($photo_id, 'full') ;
            endforeach ;
        endif ;
    ?>