• Hi all,

    I would like to display the images attached to each post outside the flow of text. I would like to have an ‘image viewer’ whereby one image (thumbnail) is displayed at a time and users can navigate through the images of each post by clicking ‘<‘ and ‘>’. I would also like to display the number of images attached to each post (i.e. ‘1 of 3’ for example).

    Unfortunately, I can’t seem to find a plugin that does just that? But, I found a nice function which retrieves the images for each post. Now I’m trying to figure out how I can provide navigation, rather than displaying all the images at once.

    function bdw_get_images($postId) {
    
        // Get the post ID
        $iPostID = $postId;
    
        // Get images for this post
        $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );
    
        // If images exist for this page
        if($arrImages) {
    
            // Get array keys representing attached image numbers
            $arrKeys = array_keys($arrImages);
    
            // Get the first image attachment
            //$iNum = $arrKeys[0];
    	echo sizeof($arrImages);
    	foreach ($arrKeys as $iNum) {	
    
               // Get the thumbnail url for the attachment
               $sThumbUrl = wp_get_attachment_thumb_url($iNum);
    
               // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL
               //$sImageUrl = wp_get_attachment_url($iNum);
    
               // Build the <img> string
               $sImgString = '<a href="' . get_permalink() . '">' .
                             '<img src="' . $sThumbUrl . '" width="50" height="50" alt="Thumbnail Image" title="Thumbnail Image" />' .
                             '</a>';
    
               // Print the image
               echo $sImgString;
    	}
        }
    };

    I would really appreciate any hints/tips/guidance/suggestions!!!

    Thanks so much for reading!

    Amal

  • The topic ‘Custom image viewer for images attached to posts’ is closed to new replies.