Custom page template to display all image attachments

I am tryin to insert all pictures in media to page. By meaning all pictures, everything added in blog. I ve been looking this feature all day. Is there any shortcode for that or something easy to make it?

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. 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

    1. Be sure that template-all-images.php is saved to your Theme directory under wp-content/themes/{theme-name}.
    2. Create a new static page
    3. Under the Page Attributes meta box, assign the “All Images” template to the page
    4. Publish the page

    Edits

    Edits incorporated into the question.