wp_get_attachment_image_src
will always return a URL to a file if the image exists in some size. If the size you ask for doesn’t exist then you get the URL for the full size image as uploaded. (This isn’t the “large” image size.)
So, for a valid image ID, your conditional if(wp_get_attachment_image_src($id, array($preferred_size, $preferred_size)))
will always succeed, as wp_get_attachment_image_src
always returns a URL, even if that URL isn’t the one you expect.
To fix it, I’d recommend that you don’t subvert the API and that you work with it instead.
Related Posts:
- What is a good alternative to using $content_width for image optimization?
- WP Responsive images – upload custom image sizes
- Where are the resized images coming from?
- image resizing on post content without css and html tags
- Optimising uploads folder then re-uploading?
- My authors need to be able to preview their upload images and manipulate and scale
- Lazy Loading of all Images
- SQL: How to find all attachments that are not used in any posts/pages/custom_post_types
- What is the best way to host a site with many images without it being too slow?
- Run command to optimize images after they get uploaded to WordPress
- Load Webp Version of Image if it exists
- Serve images as webp if browser support
- show hide image script after 4 seconds [closed]
- How do I disable responsive images in WP 4.4?
- Set default image sizes in WordPress to hard crop
- After Moving a Site to Another Domain, All Images Are Lost
- How to find attachment by it’s name?
- Change html structure of all img tags in WordPress
- How to disable “insert into post” only when selecting the Featured Image?
- Is there a way to define a default caption to all uploaded images
- How can I add the “Use as featured image” to a custom metabox?
- How to increase media image scale limit?
- Dynamic-Image-Resize Plugin does not output anything
- Cannot access the thumbnails of attachment images… Cannot find the reason
- How to place an image into header.php? [closed]
- Uploading an image as featured image from frontend form
- Add a featured image in my theme?
- Check if page/post has any anchors that link to an image jpg/gif/png
- Image File Names
- Images in upload folder with hexadecimal names
- How to check if user is uploading/setting an image as a featured image?
- JavaScript and images files are not recognized
- How to extract image width from add_image_size?
- Link to full size image or attachment page in photo theme
- How to remove Geo Tagging on image uploads, but not other EXIF data?
- Trying to create a mouseover effect in html using Gutenberg editor
- How to output images as figure/figcaption
- Make Images appear in the same order as in media browser using get children?
- wordpress get gallery images title
- How to wrap normal image tag with figure HTML5 element
- How to fix “.webp” being auto-added to the end of image urls
- How to find images in lazyload to set alt attribute?
- having issue while uploading An error occured in the upload. Please try again later
- How to change image atributes right before an image to be saved?
- Is it possible to put images as url in document [closed]
- Creating ‘required’ fields in the ‘add media’ window
- How to replace images?
- The alt=”” attribute is empty
- Add custom text in the media library image meta area
- how to move from content and place them elsewhere on a page template?
- Where is the right place to store custom images?
- Images give 404 after changing default domain
- Load Gallery Images with Title on Homepage BX Slider
- Resize images in batch
- how to get image full path from WP_Post Object?
- Advice on image heavy site, compression and slow page loads
- Not seeing text alignment option in Image block
- wp_get_attachment_url images in wrong order [closed]
- Blurry images when using add_image_size with crop
- Auto delete pictures in media library by age
- Migrated site – Images have 404 error
- Show (print) featured image all dimensions (height, width)
- Speed Up Website With High Resolution Images
- How to get image URL from media_sideload_image?
- Can’t get text to wrap around image
- Allow SVG in WP step by step
- Serve different images per screen size and cache possible?
- Auto-crop width, not height
- Convert featured images into “product images”
- How come the images arent loading properly?
- How to use the thumbnail size with catch_that_image
- Not able to retrieve image id from sibling site within a switch_to_blog
- How to display a warning when an uploaded image is too small?
- Large uploaded images not resizing?
- Having an issue in compressing Images in wordpress site
- How to generate alt attributes with php / filters?
- Word press cutting sliding images – not displaying the entire image
- Image links broken / “DNS Update Required” message
- Convert multiple image blocks to gallery block in Gutenberg?
- WordPress After migration some images src is empty
- Image sizes reset when changing theme
- How to get featured image of last post in a category?
- How can I add “alt” tag to element?
- After moving a site to new hosting, not all images are showing in media library
- Recovered WordPress installation (on localhost) links broken and images not showing
- Custom Background for each page and post
- How can the alt be added to this wooCommerce category image?
- Media sizes do not appear
- How to make Facebook share the featuterd image from url on WordPress
- Delete images of a particular size
- How to watermark wordpress images from wp-content/uploads folder with htaccess and php?
- Images not rendering
- Retrieving Image Storage path
- Add category description to list of category with taxonomy image
- How do I get the URL of only the first picture in a WordPress post?
- Specify what to do for a specific image number of the post
- Images getting corrupt automatically
- Thumbnail size keeps resetting automatically
- I am having trouble displaying an image at the top of a widget area [closed]
- Can’t upload svg files in WordPress