Are you looking for a way to output all of your attachments into an unordered list of thumbnails linking to the full-sized images? While there’s probably a plugin for this, we have created a quick code snippet that you can use to output all attached images as a gallery in WordPress.
Instructions:
All you have to do is add this code to your theme’s single.php file or in a site-specific plugin:
$args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts( $args ); if ( $attachments ) { echo '<ul id="gallery" class="thumbs">'; foreach ( $attachments as $attachment ) { $large_image = wp_get_attachment_image_src($attachment->ID,'large',false); echo '<li><a href="' . $large_image[0] . '">'. wp_get_attachment_image( $attachment->ID, 'thumbnail' ) .'</a></li>'; } echo '</ul>'; }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 20 best WordPress church themes and how to secure your WordPress forms with password protection.
Comments Leave a Reply