Thursday, July 15, 2010

Post Pic Display all thumbs related to a specific page on a media page in WordPress

Today, here is a nice piece of code to display all thumbs related to a specific page on a media pages. Very nice for galleries!
Simply paste the following function into your functions.php file:

function wallthumb($id=false,$beforelist='
<ul class="gallerythumb">',$afterlist='</ul>',$beforeitem='<li>',$afteritem='</li>')
{
global $wp_query;
$goquery = $wp_query->post;//contenu de la requète
if(is_attachment()){
$ptitre = $goquery->post_title;//le titre de l'image
$idparent = $goquery->post_parent;//l'id de la page parente
if(!$id){//si pas d'id en argument, on tente de la recuperer
$id = $goquery->ID;
}
if($idparent == null || $idparent == '' || $idparent == 0){
return;//si l'image est orpheline (sans page parente) on stop la fonction
}

//$twice = get_posts('post_type=attachment&post_mime_type=image&numberposts=-1&order=ASC&post_status=null&post_parent='.$idparent);//recup des infos des pièces jointes a la page parente
$twice = get_children('post_type=attachment&post_mime_type=image&order=ASC&post_parent='.$idparent);//recup des infos des pièces jointes a la page parente
$stocklienimage = array();//pour stocker les liens images
if($twice){//si pièces jointes
foreach ($twice as $value) {//boucle
$classthumbactu = '';
if($value->ID == $id){
//detection de l'image courante dans la boucle pour ajout d'une classe pour la differencier
$classthumbactu=' thumbactu';
}
$stocklienimage[$value->ID] = $beforeitem.'<a class="wallthumb'.$classthumbactu.'" href="'.get_attachment_link($value->ID).'" title="'.wp_specialchars( get_the_title($value->ID), 1 ).'" rel="attachment">'.wp_get_attachment_image( $value->ID, 'thumbnail' ).'</a>'.$afteritem;
}
}
else{

return;
}
echo $beforelist.implode('', $stocklienimage).$afterlist;//affichage de la liste
}
}
Once done, you just have to call the function:
<?php wallthumb() ?>

No comments:

Post a Comment