WP-Mix

A fresh mix of code snippets and tutorials

Display Images with User Submitted Posts

Here is a quick recipe for displaying linked images for images submitted with the User Submitted Posts plugin.

To display all submitted images wrapped with links (think lightbox support), add this snippet to the WordPress Loop:

<?php // USP display linked images
$args = array('order' => 'ASC','post_type'=>'attachment','post_parent'=>$post->ID,'post_mime_type'=>'image','post_status'=>null,'numberposts'=>-1,); 
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo '<a rel="lightbox" href="'.wp_get_attachment_url($attachment->ID).'" title="'.get_the_title().'">'.wp_get_attachment_image($attachment->ID,'medium',false).'</a>';							
	}
} ?>

Customize as needed to suit your implementation. Also check out the Pro version of User Submitted Posts at Plugin Planet. USP Pro provides a wealth of template tags and shortcodes to display virtually any submitted content anywhere in your theme.

You may also want to check out these related posts: Display all images attached to post and Set attachment as featured image.

Update! User Submitted Posts now features options to auto-display content, including images and other fields. Check out the plugin settings and documentation for more information. So you can auto-display all attached images without messing with any code :)

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress