1

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?

3

I think you want wordpress setting for media

for medium size their are

get_option('medium_size_w');//width

get_option('medium_size_h');//height

all setting store in options table.get more media setting

0

PHP has a function called getimagesize() which returns the an array having width as index 0, height as index 1 and other attributes.

$x = getimagesize('image.jpg');

print_r($x);

Output:

Array
(
    [0] => 245 <--width
    [1] => 112 <--height
    [2] => 2
    [3] => width="245" height="112"
    [bits] => 8
    [channels] => 3
    [mime] => image/jpeg
)
  • Thanks. But that will take the grabbed image size, not the site's Large, Medium and Thumbnail image size declarations. Na? – Mayeenul Islam Jun 13 '13 at 11:27

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.