• I have written a routine which finds the first image of the gallery, but what I couldn’t fix is finding the thumbnail version. How can I do this?

    This is the code I’ve written:

    <?php
    						$querystr =
    						"
    							SELECT
    									wp_posts.post_excerpt AS 'imageTitle'
    								,	wp_posts.guid AS 'imageGuid'
    							FROM
    								wp_posts
    							WHERE
    									wp_posts.post_parent = $post->ID
    								AND	wp_posts.post_type = \"attachment\"
    							LIMIT 1
    						";
    
    						$pageposts = $wpdb->get_row($querystr);
    						?>

    With this I can simple post an image url, like this:
    <?php echo $pageposts->imageGuid; ?>

    Again: this finds the image in the post, but not the thumbnail. Is there another way or function I can use?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello, this is what’s working for me. May be some bugs I missed but so far so good. I’m using your above query and also sorting by menu_order so that if it’s pulling from a gallery it grabs the first image that YOU set as the first image (using the gallery’s menu order feature) regardless of what order it appears in the mysql db.

    Just use:
    <?php first_image(80, 80, 1)?> within the loop. Parameters are ($width, $height, $crop_when_zooming?).

    Also you can call any of the four functions directly. The get_* functions will return the just the image’s src element without echoing the surrounding <img> tag.

    first_image(*,*) conveniently makes the <img> tag for you.

    Here’s the working source:
    Make sure you have timthumb.php in your theme’s [theme_dir]/scripts folder.

    //return the first image, <img/> tag and all.
    function first_image($width=100,$height=100,$zoomcrop=1){
    	$timthumb_url = get_first_image($width, $height, $zoomcrop);
    	if(isset($timthumb_url)){
    		?>
    		<a href="<?php the_permalink()?>" class="thumbnail" title="Links to: <?php the_title()?>">
    			<img src="<?php echo $timthumb_url?>" alt="<?php the_title() ?>" class="thumbnail"/>
    		</a>
    		<?php
    	}
    }
    
    //return just the src for the first image whether that be in the content or an attachment
    function get_first_image($width=100,$height=100,$zoomcrop=1){
    	$tn_src = get_first_content_image();
    	if (!isset($tn_src)) //no image? try getting an attached image instead (native media galleries inserted into posts use attachments)
    		$tn_src = get_first_attachment();
    
    	if(isset($tn_src))
    		$timthumb_url = get_bloginfo('template_url').'/scripts/timthumb.php?src='.$tn_src.'&h='.$height.'&w='.$width.'&zc='.$zoomcrop;
    	else
    		unset($timthumb_url);
    
    	return $timthumb_url;
    }
    
    //return just the src for the first image buried in the post's textual content.
    function get_first_content_image() {
    	global $post, $posts;
    	$first_img = '';
    	$url = get_bloginfo('url');
    	ob_start();
    	ob_end_clean();
    	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    	$first_img = $matches [1] [0];
    
    	$not_broken = @fopen("$first_img","r"); // checks if the image exists
    	if(empty($first_img) || !($not_broken)){ //Defines a default image
    		unset($first_img);
    	}else{
    		$first_img = str_replace($url, '', $first_img);
    	}
    	return $first_img;
    }
    
    //return just the src for the first image attachment
    function get_first_attachment(){
    	$querystr =
    	"
    		SELECT
    				wp_posts.post_excerpt AS 'imageTitle'
    			,	wp_posts.guid AS 'imageGuid'
    		FROM
    			wp_posts
    		WHERE
    				wp_posts.post_parent = ".get_the_ID()."
    			AND	wp_posts.post_type = \"attachment\"
    		ORDER BY \"menu_order\"
    		LIMIT 1
    	";
    	global $wpdb;
    	$url = get_bloginfo('url');
    
    	$post_item = $wpdb->get_row($querystr);
    	$first_attachment = $post_item->imageGuid;
    
    	$not_broken = @fopen("$first_attachment","r"); // checks if the image exists
    	if(empty($first_attachment) || !($not_broken)){ //Defines a default image
    		unset($first_attachment);
    	}else{
    		$first_attachment = str_replace($url, '', $first_attachment);
    	}
    
    	return $first_attachment;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to catch first image in Gallery?’ is closed to new replies.