lug
09
WordPress : isolare la prima immagine di un post e ridimensionarla
-
function get_first_image($maxwidth,$maxheight,$autoresize=false){
-
-
-
$first_img = $matches[1][0];
-
-
-
if($autoresize==1){
-
-
-
if($width>$height){
-
if($maxwidth>$width){
-
$newwidth = $width;
-
}else{
-
$newwidth = $maxwidth;
-
}
-
-
$newheight = ($height*$newwidth)/$width;
-
-
}else{
-
if($maxheight>$height){
-
$newheight = $height;
-
}else{
-
$newheight = $maxheight;
-
}
-
-
$newwidth = ($width*$newheight)/$height;
-
}
-
}else{
-
-
$newwidth = $maxwidth;
-
$newheight = $maxheight;
-
-
}
-
-
return "<img src="".$first_img."" alt="".$post->post_title."" width="".$newwidth."" height="".$newheight."" />";
-
-
}
-
-
}
Questa interessante funzione ci permette di prelevare la prima immagine di un post wordpress e ridimensionarla a nostro piacimento.
Utile se si vuole realizzare un template orginale al 100%.
Per utilizzarla dovete inserirla nella pagina functions.php del vostro template prima dell’ultima funzione presente.
La sintassi è la seguente : echo get_first_image(width,height,resize_auto); ?>
Resize_auto è un parametro che se impostato su true dice di ridimensionare l’immagine dinamicamente .
Width e height sono la larghezza e l’altezza massima dell’immagine .

Che cosa ne pensi?! Lascia un commento!