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

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter langsuyar

    (@langsuyar)

    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!

    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:
    * https://www.worch.com/2008/08/31/page-gallery-improvements/
    * https://www.ads-software.com/support/topic/200681
    * https://mfields.org/2008/04/26/adding-text-links-to-wordpress-gallery/

    Thread Starter langsuyar

    (@langsuyar)

    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 150×150 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.

    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; ?>" />
    					</a>
    				</p>
    				<?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.

    https://codex.www.ads-software.com/Function_Reference/wp_get_attachment_image_src
    https://codex.www.ads-software.com/Function_Reference/wp_get_attachment_url

    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.

    WPChina

    (@wordpresschina)

    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?

    > 6. Post your results. Thanks!

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

    solutions?

    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>'); ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Attachment.php doesn’t display image’ is closed to new replies.