Posts Tagged image crop

Image gallery(thumbnails) with WordPress image cropping

What about creating a small gallery of linked thumbnails, based on images inserted in a post, with cropping thumbnails?

First, create a post on WordPress and choose a category, for example, “tour”.
Then you can insert as many images as you want, defining links as you want.
You can use a custom field, to manage the title of the galery.

Publish you post, and call it on your PHP page, using the code bellow(where you can choose the number of images of your gallery and the cropping dimensions):

<? 		
if (have_posts()) : 
 global $post;
 $myposts = get_posts('numberposts=1&category_name=tour');
 foreach($myposts as $post) :
	// # here you call a custom field on wordpress
	$tourdes = get_post_meta($post->ID, 'Descrip', true);
   setup_postdata($post);
 ?>
  <h4><?=$tourdes;?></h4>
	<div id="thumbs">            	
		<? 
		$args = array(
			'post_type' => 'attachment',
			'numberposts' => -1,
			'post_status' => null,
			'post_parent' => $post->ID
			); 
		$attachments = get_posts($args);
		if ($attachments) {
			// - $tot_img : the total number of thumbnails on the gallery
			$tot_img=8;
			// - $brk_lin : number of image on each line
			$brk_lin=4;
			foreach ($attachments as $attachment) {						
			  // # here we define the max number of images
			  if(intval($cnt_img) < $tot_img){							
				echo "<span style='padding-left:3px'>";
				$imgref = wp_get_attachment_url($attachment->ID);
				$imgsrc = wp_get_attachment_image($attachment->ID, array(40,40), $icon = false);
				echo "<a href=".$imgref.">". $imgsrc ."<a>";
				echo "</span>"."\n";							
				// # here we define the line break
				$cnt_lin=$cnt_lin+1;
				  if(intval($cnt_lin)==$brk_lin){
				 echo "<br>";	
				 $cnt_lin=0;							
				  }
			   }
			   $cnt_img=$cnt_img+1;						
			}
		}
	
		
		?>
	</div>
<? endforeach;
endif; ?>
Advertisements

,

7 Comments

%d bloggers like this: