1

On the attachment page for an image, I'd like to display the image in its "large" size. The current behavior is showing the "medium" size image.

I'm using a theme I created from underscores. See my full theme code on Github.

The template file used for displaying attachment pages is single.php (I verified this using the Show Current Template plugin). The relevant part of that template file called that outputs the image on the attachment page is (in template-parts/content-attachment.php):

the_content(
        sprintf(
            wp_kses(
                /* translators: %s: Name of current post. Only visible to screen readers */
                __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'vanguard-history' ),
                array(
                    'span' => array(
                        'class' => array(),
                    ),
                )
            ),
            wp_kses_post( get_the_title() )
        )
    );

... which is basically just sanitization of the_content(). Looking at developer docs for the_content(), there doesn't seem to be a parameter to set attachment size there.

What I see in the HTML in one example attachment page is:

<div class="entry-content">
    <p class="attachment">
      <a href="https://www.mike-eng.com/sandbox/vanguard-history/content/uploads/2021/11/Myron_Rosander_Addressing_2001_Santa_Clara_Vanguard_on_Final_Day_of_Tour.jpg">
        <img src="https://www.mike-eng.com/sandbox/vanguard-history/content/uploads/2021/11/Myron_Rosander_Addressing_2001_Santa_Clara_Vanguard_on_Final_Day_of_Tour-300x225.jpg" class="attachment-medium size-medium" alt="" loading="lazy" srcset="https://www.mike-eng.com/sandbox/vanguard-history/content/uploads/2021/11/Myron_Rosander_Addressing_2001_Santa_Clara_Vanguard_on_Final_Day_of_Tour-300x225.jpg 300w, https://www.mike-eng.com/sandbox/vanguard-history/content/uploads/2021/11/Myron_Rosander_Addressing_2001_Santa_Clara_Vanguard_on_Final_Day_of_Tour-768x576.jpg 768w, https://www.mike-eng.com/sandbox/vanguard-history/content/uploads/2021/11/Myron_Rosander_Addressing_2001_Santa_Clara_Vanguard_on_Final_Day_of_Tour.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" width="300" height="225">
    </a>
  </p>
</div>

note the classes of "attachment-medium" and "size-medium" and the dimensions of my "medium" media size.

Where can I change the size displayed on attachment pages from "medium" to "large"? I can't find a setting anywhere for this. Also I would like to display the attachment’s caption (as set in the media library).

Also, in some older WordPress sites I've made, the default display was large, and they were also using the_content() to display the image. I’m not sure what's different here.

Is there a WordPress filter to set default attachment size shown on attachment pages? If so, what is it?

WordPress 5.8.2

Update: I verified in this comment thread that vanguard_history_post_thumbnail() is returning before the function outputs any HTML, and it does seem the_content() is what is displaying the attachment image. However, oddly, get_the_content() is returning an empty string.

Update 2: I removed the call of vanguard_history_post_thumbnail() in content-attachment.php. This was a red herring copied over from a previous template part file. The attachment image is still displaying without it.

3
  • These are "unattached" images with no parents. Not sure if it makes a difference.
    – Mike Eng
    Nov 21 '21 at 2:33
  • does the theme have an attachment.php template? developer.wordpress.org/themes/basics/template-hierarchy/… ? you might need to create and edit this accordingly.
    – Michael
    Nov 22 '21 at 17:57
  • @Michael The theme is using single.php which calls templates/content.php and uses the code snippet I posted originally, which contains the_content() to display the attachment page content, but I do not see an option there to change the attachment size. I updated the question with links to the theme code and relevant template files.
    – Mike Eng
    Nov 24 '21 at 13:57
2
+50

I've tested the following and it works for me. This uses the image's Title for the 'alt' text and displays the image's caption below the image itself. Use this in content-attachment.php, replacing the current the_content function:

$attachment_id = get_the_ID();
// If this attachment is an Image, show the large size 
if ( wp_attachment_is_image( $attachment_id ) ) {
    $attachment_image = wp_get_attachment_image($attachment_id, '', '', array('class' => 'post-thumbnail', 'size' => 'large', 'alt' => the_title_attribute(array('post'=>$attachment_id,'echo'=>0)) ) );
    echo $attachment_image;
    echo '<div class="caption">' . get_the_excerpt() . '</div>';
} else {
    // Normal display for other mime types like PDFs
    the_content(
        sprintf(
            wp_kses(
                
                __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'vanguard-history' ),
                array(
                    'span' => array(
                        'class' => array(),
                    ),
                )
            ),
            wp_kses_post( get_the_title() )
        )
    );
}
6
  • Tried that github.com/mrengy/vanguard-history/commit/… , and it didn't change the attachment page at all. It does seem the_content() is what is showing the attachment. I put in commented HTML which is in the code of the example attachment page of <!-- this is where the_content() is called in content-attachment.php -->. See it by inspecting mike-eng.com/sandbox/vanguard-history/dscn1517 . Also I commented out the_content() on content-attachment.php, and the attachment disappeared on page (I didn't pull that to the staging site).
    – Mike Eng
    Nov 29 '21 at 23:38
  • Interestingly you are the second person to suspect vanguard_history_post_thumbnail() , which gives me pause, but I am pretty sure based on the above comment and what I tried on the other answer, that alone is not the solution.
    – Mike Eng
    Nov 29 '21 at 23:40
  • I found that the function vanguard_history_post_thumbnail() stops early since it meets the conditional of if ( post_password_required() || ! has_post_thumbnail() ). "has_post_thumbnail()" is false when called from that page, so even putting the main gist of this answer using the_post_thumbnail() directly on content-attachment.php does not display the thumbnail. Screenshot share.getcloudapp.com/wbu8dvwx and code from when I got that github.com/mrengy/vanguard-history/commit/… (though this is not pulled to the staging site)
    – Mike Eng
    Nov 30 '21 at 1:11
  • I removed the call to vanguard_history_post_thumbnail() from content-attachment.php, and the attachment still displays on the attachment page. I noted this update in the original question.
    – Mike Eng
    Nov 30 '21 at 2:21
  • OK @MikeEng I think I figured it out - see update to my answer above. Attachments it turns out are really weird :)
    – Michelle
    Nov 30 '21 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.