Attachment.php doesn't display image (9 posts)

  1. langsuyar
    Posted 7 years ago #

    Trying to add an attachment template to my blog.

    I made a copy of index.php, renamed it to attachment.php. The template is used correctly when clicking on a gallery entry - but it doesn't display the image in the attachment! Weird.

    Any ideas? The code is identical to index.php (which works), and both do a standard the_content() call. When comparing the HTML that index.php and the (theoretically identical) attachment.php generate, the latter one doesn't contain any references to my image.

  2. langsuyar
    Posted 7 years ago #

    Turns out that the Codex is wrong about the hierarchy, it says that it's Index.php <- Attachment.php <- ... when in reality, Single.php is used before (so the correct hierarchy is Index.php <- Single.php <- Attachment.php <- ...).

    But that doesn't really help me, as I still get the same problem: when I make a copy of Single.php and use it as Attachment.php, my images aren't displayed even though the code is identical. That also happens when I make Image.php.

    If this is a bug I'd like to report it, but not without a few more data points. If you have a couple of minutes, please help verifying whether Attachment.php works for your WordPress blog!

    1. Open Single.php
    2. Save As Attachment.php
    3. Upload Attachment.php to your server
    4. Navigate to any gallery on your server (anywhere where you used the [Gallery] shortcode)
    5. When you click on an thumbnail, does the resulting attachment page display an image? If not, does it do so after you delete Attachment.php from the server again?
    6. Post your results. Thanks!

  3. n1ce
    Posted 6 years ago #

    Tried this, confirmed this bug. Attachment.php does not display. Deleted it, now it works again.

    The gallery function is cool, but it really doesn't make sense for anyone to have the thumbnail link to a lame "attachment" page and then the full size image is bare not inside the template. And no options in wp-admin for configuring gallery/attachment/image display...

    References for improving this situation:
    * http://www.worch.com/2008/08/31/page-gallery-improvements/
    * http://wordpress.org/support/topic/200681
    * http://mfields.org/2008/04/26/adding-text-links-to-wordpress-gallery/

  4. langsuyar
    Posted 6 years ago #

    Thanks for verifying! I filed a bug for this, btw.

    The gallery function in WordPress is far from perfect, but personally I like it better than NextGen gallery, for example. Been annoyed by the three-step process of 150x150 thumbnail->Gallery page with medium image->Click for fullsize image, but I noticed that most people don't really need to see the full-size image. They'll enter the attachment page and find that image to be big enough (at least if change the options to make the medium-size fill out the entire page). And as long as they can then browse to the other image attachment pages (without having to go back to the post as you have to in original WordPress!) they seem to be happy.

  5. jmini
    Posted 6 years ago #

    Turns out that the Codex is wrong about the hierarchy, it says that it's Index.php <- Attachment.php <- ... when in reality, Single.php is used before (so the correct hierarchy is Index.php <- Single.php <- Attachment.php <- ...).

    What appears to me is that:
    if you use the single.php template, the medium size image (+ a link to the original image) is shown by the function the_content();
    and if you use the attachement.php template it is not.

    This might be a bug, but it also let you more possibility for the customization of your attachment page.

    Here is some code that you should place over the <?php the_content(); ?> line in attachment.php file, in order to have the same display as with the single.php template.

    <?php if (wp_attachment_is_image($post->id)) {
    				$att_image = wp_get_attachment_image_src( $post->id, "medium");
    				<p class="attachment">
    					<a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>">
    					<img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" height="<?php echo $att_image[2];?>"  class="attachment-medium" alt="<?php $post->post_excerpt; ?>" />
    				<?php } ?>

    You can now customize it, by choosing the size of the picture in the wp_get_attachment_image_src() function, or not displaying the link.


  6. Justin Tadlock
    WordPress God
    Posted 6 years ago #

    I wouldn't call this a bug. It's just that single.php and index.php will handle the display for you if a template isn't available. Your attachment template isn't handling attachments correctly.

    After jmini's code, you should also place this:

    <?php the_content(__('Continue reading') . ' ' . the_title('"', '"', false)); ?>
    <?php wp_link_pages("before=<p class='pages'>".__('Pages:')."&after=</p>"); ?>

    This way, it'll appropriately output your image description. Plus, it'll handle the <!-more--> and <!--nextpage--> tags, which can be used when writing your image's description.

    I would also save this as image.php. Use attachment.php for other attachment types.

  7. WPChina
    Posted 6 years ago #

    If I use "thumbnail" or "medium" in the code below from jmini, I still only see the full-size image.

    $att_image = wp_get_attachment_image_src( $post->id, "medium");
    Or use this:
    $att_image = wp_get_attachment_image_src( $post->id, "thumbnail");

    I also removed the height and width because they were coming out as "0" for each, but I still only see the full-size image. Is there something I'm doing wrong when I upload the image? Or a setting somewhere that is wrong?

  8. paamayim
    Posted 6 years ago #

    > 6. Post your results. Thanks!

    attachment.php and/or image.php are ignored, single.php is always used instead.


  9. sabbry
    Posted 6 years ago #

    Jmini's code isn't working in WordPress 2.8.x and I'm having the same problem as the Original Poster.

    This works, but obviously it turns the image into a link, which I don't want. But it's the only thing that works so far.

    <?php $attachment_link = get_the_attachment_link($post->ID, true, array(515, 800)); // This also populates the iconsize for the next line ?>
    <?php $_post = &get_post($post->ID); $classname = ($_post->iconsize[0] <= 128 ? 'small' : '') . 'attachment'; // This lets us style narrow icons specially ?>
              <?php echo $attachment_link; ?><br />
    				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic