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() ?>

Enjoy your gallery! Note that another version of the same code is available here.

Thanks to Julien Chauvin for this great piece of code!

7 Responses

Nov 07 2009 20:02

Seems like a pretty awesome trick! Very hassle-free for the end user :)

Nov 16 2009 18:31

Looks great! I can see some sweet potential here for previewing gallery pages.

Nov 19 2009 21:25

That is a really cool hack and will come in very handy for image/photo based blogs.

Dec 26 2009 19:16

I like this feature of displaying thumbs.. As a newbie could you explain the following..

Once done, you just have to call the function:

How do I do this..

Thanks..

Jan 18 2010 00:07

David,

It means that you put the php wallthumb function into your page template that you want the thumbs to appear. Hope this explaination helps!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required