1

I am in the process of putting together my own WordPress Theme. Up until now, I have been using the 'single.php' file to act as a Template for both my Blogs and Media Files. In reference to my Media files, I have been using the <?php the_content(); ?> tag to call such images.

I have now reached the stage, where I have created an 'attachment.php' file for the various Media files. I have noticed that the <?php the_content(); ?> does not work in said file, unlike in the 'single.php' file.

What is the relevant alternative to <?php the_content(); ?> when it comes to calling images in the 'attachment.php' file?

0

Calling the_content from within attachment.php will bring you the Attachment Description.

Put the code below on your page and see what you can get from within attachment.php file:


while ( have_posts() ) : the_post();

    /**
     * @link    https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata#Related
     */
    $data = array(
        get_the_ID(), // Gets att ID.
        get_permalink(), // Gets att page link.
        get_the_title(), // Gets att title.
        get_the_content(), // Gets att description.
        wp_get_attachment_metadata(), // Gets att metadata.
    );

    var_dump( $data );

endwhile;

Also, you might want to take a look at https://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata#Related and see other functions that will perhaps be useful for you.

  • Thanks for your input. Unfortunately, your code produces a long message starting with array(5) { [0]=> int(237) [1]=> string(39) "http://www.example.com/leather-brown/" [2]=> string(13) "Leather Brown" [3]=> string(0) "" [4]=> array(5). How could I resolve this? Thanks for the additional link, also. – Craig Mar 1 '17 at 2:00
  • You said you wanted to bring the image within your template. So by calling var_dump in the above case you should have seen the image source for the media attachment. Now you just do whatever you want to do with it. – filipecsweb Mar 1 '17 at 2:05
  • Maybe I didn't explain fully. I want to be able to upload an image via the WordPress Dashboard and then able to view that image within its own URL. I can call it via the 'single.php' file but the tag does not work within the 'attachment.php' file. – Craig Mar 1 '17 at 2:15
  • Did you access the link I sent you in my answer? Did you see the image source link? Did you try calling the function wp_get_attachment_image()? – filipecsweb Mar 1 '17 at 2:17
0

Here's one way to do it:

<?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "full"); ?>

<a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>" rel="attachment">
<img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" height="<?php echo $att_image[2];?>" alt="" />
</a>

<?php endif; ?>

Use it inside the loop. Above example first checks if the attachment is an image.

Or you might also want to just use wp_get_attachment_image() to output the entire image tag:

https://developer.wordpress.org/reference/functions/wp_get_attachment_image/

There's also get_atached_media that you can use.

https://codex.wordpress.org/Function_Reference/get_attached_media

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.