Posts Tagged lists

Display WordPress Post List With Custom Size Thumbnails(the first image of each post)

This post moved to http://www.wpworking.com/general/display-wordpress-post-list-with-custom-size-thumbnails/

Note:

Maybe you will like to take a look on my Get Post List With Thumbnails plugin, wich is a evolution of the function bellow and you can configure and use as widget.

The function bellow, shows a list with posts and custom size thumbnails(for the post first image), linked to each post permalink. You can use it wherever you want, and you can choose a category(not required) and the number of posts(not required). Two simple steps:
A função abaixo mostra uma lista de posts com thumbnails de tamanho customizável(para a primeira imagem de cada post), linkados ao link permanente de cada post. Você pode usar onde quiser, e pode escolher uma categoria(não obrigatório) e o número de posts(não obrigatório). Dois passos simples:

1) Put this function on “functions.php” file, in your template folder:
1) Coloque esta função no arquivo “functions.php”, na pasta de seu template.

<?
// display a list of posts with custom size thumbnails, using the post first image
function getPostList($categ='',$postnr=20){
	// $categ = category name - not required
	// $postnr = number of posts you want to display - not required
	$htmlcod .= "<div id='div_postlist' style='border:1px red solid'>"."\n";
	// 
	if (have_posts()) : 				
		global $post;
	    // here we check if the user has chosen a category
	    if($categ!=''){
	      $strquery = "numberposts=".$postnr."&category_name=". $categ;				
	    }
	    else{
	      $strquery =  "numberposts=".$postnr;
	    }
			 
	    // here we get the posts
	    $myposts = get_posts($strquery);
			 		 
	    // if we want, we can display the number of registers found
	    //echo sizeof($myposts);
			 
	    if($myposts):
		 // here we go in the loop
		foreach($myposts as $post) :				 	
                    // getting the first image of the post to make the thumb
                    $args = array(
                        'post_type' => 'attachment',
                        'numberposts' => -1,
                        'post_status' => null,
                        'post_parent' => $post->ID
                    ); 
                    $attachments = get_posts($args);
                    
                    // here we set the variable for the attachment string
                    $imgsrc = "";
                    if ($attachments):
                        // here we take the first image and break the loop
                        foreach ($attachments as $attachment) {
                             // this brings the attachment array $imgobj = wp_get_attachment_image_src($attachment->ID);
                             // if you use the line obove, you can call $imgobj[0] to get the image source 
                            // you can set the thumbnail dimensions, here we use 40 x 40
                            $imgsrc = wp_get_attachment_image($attachment->ID, array(40,40), $icon = false);
                            break;
                        }
                    endif;
                    // here we start to build the return html code
                        $htmlcod .= "<p><a href='". get_permalink()."' title='". get_the_title() ."'>";
                        $htmlcod .= get_the_time('d/m/Y')."-".get_the_title();
                        $htmlcod .= "</a></p>"."\n";	
                    // if the post has at least one image attached, we display it
                    if($imgsrc!=""):
                        $htmlcod .= "<p>";                                	
                        $htmlcod .= "<a href='". get_permalink() ."' title='". get_the_title() ."'>";
                        $htmlcod .= $imgsrc; 
                        $htmlcod .= "</a>"."\n";       
                        $htmlcod .= "</p>"."\n";															
                    endif;								
		endforeach;     
	    else:
			// here goes the message, if there is no results to display
			$htmlcod = "<div>"."\n";
			$htmlcod .= "<p>No registers found.</p>"."\n";								
			$htmlcod .= "</div>"."\n";
	    endif;			
	endif;
        $htmlcod .= "</div>";
	return $htmlcod;
}
?>

2) Call the function wherever you want:
2) Chame a função, onde quiser.

// e.g. - no category, twenty(20) posts 
<? echo getPostList('',20); ?>

or

// e.g. - 'musicposts' category name, no number of posts, but the function uses 20 as default
<? echo getPostList('musicposts');  ?>

or

 // e.g. - 'gameposts' category name, no number of posts, 5 posts
<? echo getPostList('gameposts',5); ?>

Hope you find it useful.

Advertisements

, , ,

12 Comments

%d bloggers like this: