Posts Tagged images workaround

Pass form values to WordPress query_posts(category name / category id) function to retrieve posts from a specific category

I was taking a look at WordPress Support Forum and found this doubt, about how to pass form values to the query_posts function to retrieve posts from a specific category.
Eu etava dando uma olhada no Forum de Suporte WordPress e achei esta dúvida, sobre como passar valores de formulários para a função query_posts, para retornar posts de uma categoria específica.

First of all, there’s a difference between working with the category name and the category id as parameters:
Primeiramente, há uma diferença entre trabalhar com o nome da categoria e o id da categoria como parâmetros:

//working with the category name
$catnam = $_GET["catnam"]; // or $_POST["catnam"]; // using GET or POST depends on your form submit method
query_posts("category_name=$catnam");

//working with the category id
$catid = $_GET["catid"]; // or $_POST["catid"]; //using GET or POST depends on your form submit method
query_posts("cat=$catid");

Well, now let’s see the complete code.
Bem, agora vamos ver o código completo.

<?
//category name or id
$catid = $_GET["catid"];

// passing the parameters to query_posts
query_posts("cat=$catid");

if (have_posts()) :
     while (have_posts()) : the_post();				 	
?>
     <div>
       <h3><a href="<?php the_permalink();?>"><?=get_the_title();?></a></h3>
     </div>
     <div>
       <p><?=get_the_content();?></p>
     </div>
<?
    endwhile;     
else:
?>
     <div>
       <p>No registers found.</p>								
     </div>
<?
endif;
?> 

Maybe you will enjoy taking a look on another post, where I talk about page navigation on Category Pages.
Talvez você curta dar uma olhada em outro post, onde eu falo sobre navegação de página em Páginas de Categoria.

,

2 Comments

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 = "";
	}
	else{
		$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) {
	/*
		Warning:
		# 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!
		alvaron8@gmail.com
	*/
	// 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];
		if($link_img!=""):
			// 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));
		endif;
		// #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);
	else:
		$postOutput=$content;
	endif;
	return $postOutput;
}

And call it on your code, like this:

echo clear_first_image(get_the_content());

,

14 Comments