I’m making a custom function for my WordPress site.
function mediumFeaturedImage() {
if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg" alt="No Image Given" width="220" height="145"/>
<?php }
}
I set the medium
size images to 220 x 145
in Settings > Media
.
But if you look at the default ‘no-image’ code block here, I used hard-coded numerals as width="220" height="145"
. But I want to use this function into some other projects of mine. And I won’t use the same settings for medium sized images always. It’ll be different from site to site. And always I want my code to be dynamic. Therefore I want a code like this:
<img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg" width="<?php $getimagesize('medium','width); ?>" height="<?php $getimagesize('medium','height); ?>"/>
It’s not mandatory using a function like me. Is there a built-in code like this, that will grab the sizes I assigned in Media settings in WP-Admin, always?
Or, by using the wp_get_attachment_image_src()
how can I grab the width and height of medium size image only, that assigned for medium sized images in Admin panel?
Possible?
I think you want wordpress setting for media
for medium size their are
all setting store in options table.get more media setting
PHP has a function called
getimagesize()
which returns the an array havingwidth
as index0
,height
as index1
and other attributes.Output: