$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>post thumbnails - Display featured image file size, extension, filename, type, orientation?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

post thumbnails - Display featured image file size, extension, filename, type, orientation?

matteradmin8PV0评论

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?

Share Improve this question asked Jun 28, 2014 at 13:19 Advanced SEOAdvanced SEO 6892 gold badges16 silver badges41 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

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
Post a comment

comment list (0)

  1. No comments so far