0

I am nearly there but need the help of the community to bring me home! I am linking from a "gallery overview" page (thumbnail grid) to a single, full-size image page (attachment.php) containing a slideshow. The user should then be able to use flexslider to scroll through the additional attached images. So far, this is all working correctly—in principle.

The problem is that clicking on any thumbnail brings the user to the FIRST image in the gallery, rather than the actual image represented by the thumbnail. My attachment.php code is as follows:

<div id="slider" class="slider">
    <ul class="slides-init">
<?php
            global $post;
            $args = array(
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_status' => null,
                'post_parent' => $post->post_parent,
                'order_by' => 'menu_order',
                'order' => 'ASC'
            );
            $attachments = get_posts($args);
            if ($attachments) {
                foreach ($attachments as $attachment) {
                    echo '<li>';
                    echo wp_get_attachment_image($attachment->ID, 'large');
                    echo '</li>';
                }
            }
?>
    </ul>
</div>

Any help would be greatly appreciated. Also, the test site can be found here (simply click on one of the thumbnails to see what I mean):

http://www.adamprince.us/clients/mcnairevans/projects/confessions-for-a-son/

Many thanks!!!

  • How to get flex slider to go to a specific slide at the start is offtopic, as it's a javascript question not a WordPress question. Unless this question is how to retrieve the post ID of the current linked to attachment? You can then take that ID and put it somewhere your javascript can find and then use that to set your slide ( something you would ask somewhere else, e.g. stack overflow ) – Tom J Nowell Aug 6 '14 at 23:09
  • Tom, it turns out it is a bit of both, but your point is well taken. Anyway, I have figured it out! – Adam Aug 7 '14 at 22:04
0

The answer was to assign a count to each attachment:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID,
    'order_by' => 'menu_order',
    'order' => 'DESC'
);

$attachments = get_posts( $args );
    if ( $attachments ) {
    $count = 0;
        foreach ($attachments as $attachment) {
            $link = get_permalink($attachment->ID);
            $link = add_query_arg('img',$count,$link);
                echo '<div class="thumbnail">';
                echo '<a href="'.$link.'" >';
                echo wp_get_attachment_image( $attachment->ID, 'small' );
                echo '</a>';
                echo '</div>';
                $count++;
        }
    }

And then link up with that on attachment page using the assigned number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.