1

I wanted to know if this is possible. I have gone through a lot of threads on the internet for this one and there does not seem to be one good solution. I run a business website based on wordpress and now there is this peculiar requirement wherein I need a hack/code to pull and display one single image ( from among the other images uploaded/attached to that post ).

Now the tricky part is that the image must be a horizontally/landscape oriented image only. And if it does not find a landscape image it may return nothing. Please let me know if need more clarifications.

If you must know, I already have a featured image.

6
  • @johannes. Your code seems to work. I took your code and pasted in my custom-functions.php and used the code below in my single.php and I was able to pull a url of the image. Now, what do I need to modify to the acutal image? thanks.
    – gurung
    Commented Apr 16, 2013 at 13:06
  • See the below usage example. If by "modify" you mean display it in a different size: CSS or the img tags width and height attribute are your friend. But that is for one a different issue and for another pure HTML/CSS. Commented Apr 16, 2013 at 13:15
  • @JohannesPille this code '<img src="<?php echo wpse_96012_landscape_image() ;?>" />' did the job. Now everything is perfect. You guys are good. Thank you.
    – gurung
    Commented Apr 16, 2013 at 13:15
  • While a "Thanks" is appreciated, be so kind as to tick the checkmark left of the answer if it solved your issue, such that others can later on tell that this issue is resolved. Pay it forward. Commented Apr 16, 2013 at 13:18
  • Sure, will do it right away. Also I was curious if I needed a single "vertical" image then what would change ?
    – gurung
    Commented Apr 16, 2013 at 13:21

1 Answer 1

3

Function to retrieve "landscape" oriented image

function wpse_96012_landscape_image() {

    /* global post object holding info about the current post */
    global $post;

    /* grab all images attached to the current post */
    $attached_images = get_children( array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'post_mime_type' => 'image'
    ) );

    /* set minimum ratio between width / height, change to your liking */
    $minimum_aspect_ratio = 1.2;

    /*
     * iterate over attachments
     * get size, compare width & height
     * stop execution if an image with "landscape" dimensions is found
     */
    foreach( $attached_images as $image ) {
        $url = wp_get_attachment_url( $image->ID );
        $size = getimagesize( $url );
        if ( $size[0] >= ( $size[1] * $minimum_aspect_ratio ) ) {
            return $url;
        }
    }

    return false;
}

Later Usage

The above function will return the URL of the first image with an aspect ratio higher than or equal to the set minimum or false if none was found.

Hence, if you'd like to output the image on the condition of there being one, this is how you'd go about it:

$landscape_image_url = wpse_96012_landscape_image();
if ( $landscape_image_url ) {
    echo '<img alt="landscape image" src="' . $landscape_image_url . '" />';
}

References

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.