How to display some information of featured image in post.
I need to display:
1.
a. Image file size (in kb or MB)
b. Image type (.jpg,.gif...),
c. Bare image name, with and/or without image extension (some-image-file-name.jpg).
Also, if it possible it would be nice to show this too:
d. orientation of image (landscape, portrait)
Maybe it will be helpful if I say that I do not have any image in post content, only as featured image.
It would be great to have separate functions to display that info of "medium" and image in "full" size, or at least of "full" size.
Could you please give me some functions which I can put in my theme files to display that info of featured image?
2.
I managed to show image title, caption and description of featured post image with:
echo get_post(get_post_thumbnail_id())->post_title;
echo get_post(get_post_thumbnail_id())->post_excerpt;
echo get_post(get_post_thumbnail_id())->post_content;
But, how to show image alt ("Alt Text") of featured image?
How to display some information of featured image in post.
I need to display:
1.
a. Image file size (in kb or MB)
b. Image type (.jpg,.gif...),
c. Bare image name, with and/or without image extension (some-image-file-name.jpg).
Also, if it possible it would be nice to show this too:
d. orientation of image (landscape, portrait)
Maybe it will be helpful if I say that I do not have any image in post content, only as featured image.
It would be great to have separate functions to display that info of "medium" and image in "full" size, or at least of "full" size.
Could you please give me some functions which I can put in my theme files to display that info of featured image?
2.
I managed to show image title, caption and description of featured post image with:
echo get_post(get_post_thumbnail_id())->post_title;
echo get_post(get_post_thumbnail_id())->post_excerpt;
echo get_post(get_post_thumbnail_id())->post_content;
But, how to show image alt ("Alt Text") of featured image?
Question 1: use wp_get_attachment_metadata
in the Codex
or the search function on this forum!
Question 2: Put this in one of your functions. It will echo the Alt text for the image.
$img_id = get_post_thumbnail_id( get_the_ID() ); //Note: you may need other methods to get id
$alt_text = get_post_meta( $img_id, '_wp_attachment_image_alt', true );
if (!empty ($alt_text) ) {
echo 'The Alt text: ' . $alt_text;
} else {
echo "No cigar!";
}
For 1C, you can use pathinfo() to retrieve the file information:
$file = pathinfo( 'http://your-website/wp-content/uploads/2019/03/your-photo.jpg' );
echo $file['dirname']; // http://wpmasterclass.localhost/wp-content/uploads/2019/03
echo $file['basename']; // your-photo.jpg
echo $file['extension']; // jpg
echo $file['filename']; // your-photo
If you only need filename and extension, a straightforward alternative is by using basename():
$file = basename('http://your-website/wp-content/uploads/2019/03/your-photo.jpg');
echo $file; // your-photo.jpg