Posts Tagged content

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

This post moved to


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
	      $strquery = "numberposts=".$postnr."&category_name=". $categ;				
	      $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);
		 // 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);
                    // 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
                        $htmlcod .= "<p>";                                	
                        $htmlcod .= "<a href='". get_permalink() ."' title='". get_the_title() ."'>";
                        $htmlcod .= $imgsrc; 
                        $htmlcod .= "</a>"."\n";       
                        $htmlcod .= "</p>"."\n";															
			// here goes the message, if there is no results to display
			$htmlcod = "<div>"."\n";
			$htmlcod .= "<p>No registers found.</p>"."\n";								
			$htmlcod .= "</div>"."\n";
        $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); ?>


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


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

Hope you find it useful.

, , ,


Get the first image from wordpress posts(content)

There’s a lot of ways to get the first image from wordpress posts.

I’ve found a workaround, using the function bellow. Just Put it on your “functions.php” file.

function get_first_image($content,$wdt,$hgt) {
	$first_img = '';
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	$pref_img = $matches [1] [0];

	if(empty($pref_img)){ //Defines a default image
		$pref_img = "";
		$posgif = strrpos($pref_img , ".gif", 0);
		if($posgif > 0){
			$iext = "gif";
			$posimg = $posgif;
		$posjpg = strrpos($pref_img , ".jpg", 0);
		if($posjpg > 0){
			$iext = "jpg";
			$posimg = $posjpg;
		$posjpeg = strrpos($pref_img , ".jpeg", 0);
		if($posjpeg > 0){
			$iext = "jpeg";
			$posimg = $posjpeg;
		$pospng = strrpos($pref_img , ".png", 0);
		if($pospng > 0){
			$iext = "png";
			$posimg = $pospng;
		$first_img = substr($pref_img, 0, $posimg).".".$iext."?w=".$wdt."&h=".$hgt;
	return $first_img;

Call it in your code, like this:

<img src="<?php echo get_first_image_mat(get_the_content(),230,138);?>" border="0" style="width:230px;height:138px">


Leave a comment

Remove first image from a wordpress post(the_content)

So tired… soul searching… I was looking for a solution to remove the first image from wordpress posts. I’ve found some, but they didn’t work. So, I decided to make my own code:

Just put it on your “functions.php” file.

function clear_first_image($content) {
		# I know light sabers are better...
			...but maybe I'm not good enough, so I use laser guns :)
		# It is not an elegant solution, but it works
		# Maybe someday I'll change it to use more regular expressions than logic
		# It's not recommended use this solution when you are
		processing large amount of data, like when you are on a loop
		# To call it on your code, under a loop(like "while" or "for"), use:
			echo clear_first_image(get_the_content())

		# Good luck! May the force be with you!
	// checks if there is a first image
	$postst = strpos($content, '<img');
	if($postst !== false):
		// #letz check if the image has a link and remove it
		$output = preg_match_all('/<a.+href=[\'"]([^\'"]+)[\'"].*><img.+src/i', $content, $matches);
		$link_img = $matches [1] [0];
			// we get the first link 'begin' tag
			$lnkini = strpos($content , '<a href', 0);
			// then we get the first link 'close' tag
			$lnkend = strpos($content, '<img', $lnkini);
			// letz get the hole first link tag now
			$replnk = substr($content, $lnkini, ($lnkend-$lnkini));
			// now we replace the first link tag in the content
			$tmpctd = preg_replace("'".$replnk."'",'',$content);
			// letz clear the link tag close
			$lnkcld = strpos($content , '</a>', 0);
			$content = substr($tmpctd, 0, $lnkcld).substr($tmpctd, $lnkcld, strlen($tmpctd));
		// #removing the first image
			// we get the first image 'open' tag
			$posini = strpos($content , '<img', 0);
			// then we get the first image 'close' tag
			$posend = strpos($content, '>', $posini);
			// letz get the hole first image tag now
			$repimg = substr($content, $posini, ($posend-$posini)+1);
			// now we replace the first image tag in the content
			$postOutput = preg_replace("'".$repimg."'",'',$content);
	return $postOutput;

And call it on your code, like this:

echo clear_first_image(get_the_content());



%d bloggers like this: