First, you need to create a custom page Template, to hold your custom loop output. Create a Theme file, named e.g. template-all-images.php
, with the following header:
<?php
/**
* Template name: All Images
*/
?>
Then, in the custom page template, you need to query for all image attachments. Try using WP_Query()
, with post type/status arguments:
<?php
$images_query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image'
);
$images_query = new WP_Query( $images_query_args );
?>
Then, output the query:
<?php
if ( $images_query->have_posts() ) : while ( $images_query->have_posts() ) : $images_query->the_post();
// Normal loop output goes here
endwhile; endif;
// Be kind; rewind
wp_reset_postdata();
?>
For your loop output, if you just want to output a fully formed image, you can use e.g. wp_get_attachment_image()
:
<?php
wp_get_attachment_image( get_the_ID(), 'large' );
?>
(Replace 'large'
with your desired image size, or omit, to use the default size, 'thumbnail'
.)
The entire custom page template file might look like the following:
<?php
/**
* Template name: All Images
*/
// Get the header
get_header();
// Image attachment query arguments
$images_query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image'
);
// Query image attachments
$images_query = new WP_Query( $images_query_args );
// Image attachment query loop
if ( $images_query->have_posts() ) : while ( $images_query->have_posts() ) : $images_query->the_post();
// Output the attachment image
wp_get_attachment_image( get_the_ID(), 'large' );
endwhile; endif;
// Be kind; rewind
wp_reset_postdata();
// Get the footer
get_footer();
?>
Using your custom page template
- Be sure that
template-all-images.php
is saved to your Theme directory underwp-content/themes/{theme-name}
. - Create a new static page
- Under the Page Attributes meta box, assign the “All Images” template to the page
- Publish the page
Edits
Edits incorporated into the question.