WordPress Development Asked by Craig on December 26, 2020
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?
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
Answered by ajie on December 26, 2020
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.
Answered by filipecsweb on December 26, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP