Are you looking for a way to get all image attachments from a page or post? While there’s probably a plugin for this, we have created a quick code snippet that you can use to show images from post or page attachments in WordPress.
Instructions:
All you have to do is add this code to your theme’s single.php file:
<?php $argsThumb = array( 'order' => 'ASC', 'post_type' => 'attachment', 'post_parent' => $post->ID, 'post_mime_type' => 'image', 'post_status' => null ); $attachments = get_posts($argsThumb); if ($attachments) { foreach ($attachments as $attachment) { //echo apply_filters('the_title', $attachment->post_title); echo '<img src="'.wp_get_attachment_url($attachment->ID, 'thumbnail', false, false).'" />'; } } ?>
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: 12 best WordPress plugins for bloggers and how to create WordPress optin forms.
Comments Leave a Reply