0

i have added a function like this to generate custom image size

add_action( 'after_setup_theme', 'slider_image');

    function slider_image(){
        add_image_size( 'slide_image', 540, 405);
    }

I want to store the width and height to 2 separate variables, so that i can compare it later if the featured image is less than the width or height, i can hide such posts on the blog page!

Logic to do it will be a bonus for me!

Any help to do it in best way possible will be appreciated!

3
  • if you use this field to save any post detail than, you can save this field value as add_post_meta($post_id, $meta_key, $meta_value, $unique); for update update_post_meta($post_id, $meta_key, $meta_value, $prev_value); Apr 21, 2018 at 18:04
  • Sorry, it won't work
    – Nimesh
    Apr 22, 2018 at 1:43
  • of course, this code will not work directly. this is the concept to save data. Apr 22, 2018 at 11:17

1 Answer 1

2

You can always use the wp_get_additional_image_sizes() function to retrieve an image "size" (i.e. width, height, and the 'crop' option), like this:

$image_sizes = wp_get_additional_image_sizes();
if ( isset( $image_sizes['slide_image'] ) ) {
    $size = $image_sizes['slide_image'];
    var_dump( $size ); // Demo
    echo $size['width']; // Demo
}

UPDATE

In reply to your comment,

i want to get the current post featured image width and height..how to do it

Use the wp_get_attachment_image_src() function:

The returned array contains four values: the URL of the attachment image src, the width of the image file, the height of the image file, and a boolean representing whether the returned array describes an intermediate (generated) image size or the original, full-sized upload.

— More on https://developer.wordpress.org/reference/functions/wp_get_additional_image_sizes/

But anyway, try this function:

// See https://wordpress.stackexchange.com/q/301465/137402
function my_wpse301465( $image_size, $post = null ) {
    $post_thumbnail_id = get_post_thumbnail_id( $post );

    $info = $post_thumbnail_id ?
        wp_get_attachment_image_src( $post_thumbnail_id, $image_size ) : null;

    if ( $info ) {
        $sizes = wp_get_additional_image_sizes();
        $size = isset( $sizes[ $image_size ] ) ? $sizes[ $image_size ] : null;

        return $size ? (
            $info[1] < $size['width'] ||
            $info[2] < $size['height']
        ) : false;
    }
}

And here's how you could use it:

// Or pass a post ID as in my_wpse301465( 'slide_image', 123 ).
if ( my_wpse301465( 'slide_image' ) ) {
    echo 'Hide featured image.';
} else {
    echo 'Show featured image.';
}
2
  • Thanks but, i want to get the current post featured image width and height..how to do it? Please help me!
    – Nimesh
    May 6, 2018 at 22:35
  • 1
    I've actually updated the answer. Check it out.
    – Sally CJ
    May 7, 2018 at 8:09

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.