0

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?

3

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!";

}
  • Thank you, it works.Could you please make another function to check if there is alt text at all, and, if there is to show it, and if there is no alt text to echo "something else"? – Advanced SEO Jun 28 '14 at 15:15
  • I added it above. For the if statement, take a look on W3Schools - easy stuff! – Pat Gilmour Jun 28 '14 at 15:33
  • Any help with image file size, file name, type? I still can not find right solution for that? – Advanced SEO Jun 30 '14 at 8:47
0

For 1C, you can use pathinfo() to retrieve the file information:

$file = pathinfo( 'http://your-website.com/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.com/wp-content/uploads/2019/03/your-photo.jpg');
echo $file; // your-photo.jpg

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.