syamilmj / Aqua-Resizer

Resize WordPress images on the fly
502 stars 208 forks source link

Is it possible to outputing feature image or first post image as thumbnail? #17

Closed mailmilisku closed 11 years ago

mailmilisku commented 11 years ago

Is it possible to combine feature image or first image in post as the thumbnail? In case user don't set up feature image, automatically will use the first post image as thumbnail. In traditional way, I use this code:

<?php
$size = 'thumbnail'; // whatever size you want
if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'thumb-600' );
} else {
    $attachments = get_children( array(
        'post_parent' => get_the_ID(),
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'order' => 'ASC',
        'orderby' => 'menu_order ID',
        'numberposts' => 1)
    );
    foreach ( $attachments as $thumb_id => $attachment )
        echo wp_get_attachment_image($thumb_id, $size);
    }
?>

How can I implement these code when I'm using Aqua-resizer? Or maybe you have better solution? Thanks in advance.

syamilmj commented 11 years ago

Basically, you want to to grab the first image from the attachments, so first, store all your attachments in an array like so:

$images = array();
    foreach ( $attachments as $thumb_id => $attachment )
        $images[] = wp_get_attachment_image($thumb_id, $size);
    }

then to grab the first image url,

$first_img = '';
if(!empty($images)) $first_img = $images[0];

Now you can do your conditional check:

if(has_post_thumbnail()) {
// resize post thumbnail here e.g. $img_url = aq_resize...
} elseif($first_img) {
// resize the first img here, $img_url = aq_resize($first_img, ...
} else {
// $img_url = ''; //empty
}

Hope that helps

mailmilisku commented 11 years ago

Great! Thank you, Syamil. Do you implement this code for your theme on Themeforest? Which one? If so, I want to buy.

syamilmj commented 11 years ago

Nope sorry, none of my themes does this.

mailmilisku commented 11 years ago

Will be great for the next user if you add the sample code here: https://github.com/sy4mil/Aqua-Resizer/wiki/Examples

Once again, thank for your great work.

syamilmj commented 11 years ago

It's actually a WP question, not aqua resizer's question. :)

Cheers