lautr.com

Hannes Blog for Development and Stuff besides

Display Image count like, picture x of y in WordPress

| 5 Kommentare

In a recent Situation i needed to display the attachments of an WordPress Post, the Images, in a click-through-like manner. I also decided to display the current number in relation to the full number of Images you are currently looking at, like a simple “Picture 5 of 7″. Since there is no build in Option for that I had to build my own.

function getNumberOfSiblings($parentid){
$images = (get_children(array(
'post_parent' 	=> $parentid,
'post_type' 	=> 'attachment',
'post_mime_type'=> 'image',
)));
return count($images);
}

function getCurrentNumberOfSibling($parentid,$siblingid){
$images = (get_children(array(
'post_parent' 	=> $parentid,
'post_type' 	=> 'attachment',
'post_mime_type'=> 'image',
)));
if($images[$siblingid]->menu_order != 0){
return $images[$siblingid]->menu_order;
}else{
$i = count($images);
foreach($images as $image){
if($image->ID ==$siblingid){
return $i;
}
$i--;
}
}
}

I mainly just use the get_children function, in the first case to count all the returned siblings and in the second case to get the Position of the current Attachment I use the menu_order Attribute of that matter.

Autor: Hannes

Hi! I’m Johannes Lauter a 26 year old Web Application Developer based in Berlin ... more

5 Kommentare

  1. This is a very nifty function and I’ve been looking all over for it. However, I can not get it to work. I want to use this one on the image.php template but I’ve tried everything within the scope of my knowledge but I still can’t makeit work. I think I am doing something wrong.

    Can you help me out? It would be very much appreciated. Thanks!

  2. The correct use inside the image.php would be “< ?php print getCurrentNumberOfSibling($post->post_parent,$post->ID); ?> of < ?php print getNumberOfSiblings($post->post_parent); ?>” , anyhow I’ll gonna mail you the file, hope it helps!

  3. Hi Hannes i have been looking for a function like this all over the web can u send me a file or an example who you implement the functions in to an attachment.php I use the [gallery] option that wordpress 2.5+ is offering.

    Thanks

  4. Hi Hannes I used the example from your comment and it solved the problem thanks again for this set of functions it’s great :)

  5. Hey blazo, sorry for not replying :D great that you figured it out on your own and that my little example helped’ you :)

Hinterlasse eine Antwort

Pflichtfelder sind mit * markiert.

*