Gallery Inside One Post

These functions will help:

//function to get next or previous keys in an array
function array_navigate($array, $key){
    $keys = array_keys($array);
    $index = array_flip($keys);
    $return = array();
    $return['prev'] = (isset($keys[$index[$key]-1])) ? $keys[$index[$key]-1] : end($keys);
    $return['next'] = (isset($keys[$index[$key]+1])) ? $keys[$index[$key]+1] : current($keys);
    return $return;
}

function previous_attachment_ID($att_post){
    //get the attachments which share the same post parent
    $images =& get_children('post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=".$att_post->post_parent);
    if($images){
        //get the id of the previous attachment
        $ppid_arr = array_navigate($images, $att_post->ID);
        $ppid = $ppid_arr["prev'];
        return $ppid;
    }
    return false;
}

//previous attachment link function
function prev_att_link($att_post=null){
    if($att_post == null){
        global $post;
        $att_post = get_post($post);
    }
    $ppid = previous_attachment_ID($att_post);
    if($ppid != false){
        return '<a href="' . get_attachment_link($ppid) . '" class="previous-attachment-link">Previous</a>';
    } else {
        //there is no previous link
        return false;
    }
}

function next_attachment_ID($att_post){
    //get the attachments which share the same post parent
    $images =& get_children('post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=".$att_post->post_parent);
    if($images){
        //get the id of the next attachment
        $ppid_arr = array_navigate($images, $att_post->ID);
        $ppid = $ppid_arr["next'];
        return $ppid;
    }
    return false;
}

//next attachment link function
function next_att_link($att_post=null){
    if($att_post == null){
        global $post;
        $att_post = get_post($post);
    }
    $ppid = next_attachment_ID($att_post);
    if($ppid != false){
        return '<a href="' . get_attachment_link($ppid) . '" class="next-attachment-link">Next</a>';
    } else{
        return false;
    }
}

//back to gallery link function
function back_to_gal_link($text="Back to gallery"){
    global $post;
    $post = get_post($post);
    $post_par = get_post($post->post_parent);
    $slug = get_permalink($post_par->ID);
    return '<a href="'.$slug.'">'.$text.'</a>';
}


//show all thumbnails function
function show_all_thumbs() {
    global $post;
    $post = get_post($post);
    $images =& get_children( 'post_type=attachment&post_mime_type=image&output=ARRAY_N&orderby=menu_order&order=ASC&post_parent=".$post->post_parent);
    if($images){
        foreach( $images as $imageID => $imagePost ){
            if($imageID==$post->ID){
            } else {
                unset($the_b_img);
                $the_b_img = wp_get_attachment_image($imageID, "thumbnail', false);
                $thumblist .= '<a href="'.get_attachment_link($imageID).'">'.$the_b_img.'</a>';
            }
        }
    }
    return $thumblist;
}

With this you can implement a previous next setup as you wanted, and, you can show all the thumbnails in the gallery.

e.g.:

<div class="back_to_gal_link">
    <?php
    echo back_to_gal_link().'<br />';
    ?>
</div>
<div class="prev_next_links">
    <?php
    if(prev_att_link()){
        echo prev_att_link();
    }
    if(prev_att_link() && next_att_link()){
    //insert a seperator between the two links
        echo ' | ';
    }
    if(next_att_link()){
        echo next_att_link();
    }
    ?>
</div>
<div class="thumbnails">
    <?php
    echo show_all_thumbs();
    ?>
</div>

Leave a Comment