Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this code which displays images with their captions in a list. On clicking caption, opens image in an attachment page, but clicking on image, opens that image with a dark-gray background. Is there a way to display image on attachment page on clicking on it?? I played alot with it, but no success.

$i = 0;
    $rand_id = mt_rand(1,1000);
    foreach ( $attachments as $id => $attachment ) {
        $image_attributes = wp_get_attachment_image_src( $id,'large' );


        $link = isset($attr['link']) && 'file' == $attr['link'] ? '<a href="'.$image_attributes[0].'"  rel="prettyPhoto[pp_'.$rand_id.']"><span class="mosaic-overlay"></span>'.wp_get_attachment_image($id, $size, false).'</a>' : '<a href="'.$image_attributes[0].'"  rel="prettyPhoto[pp_'.$rand_id.']"><span class="mosaic-overlay"></span>'.wp_get_attachment_image($id, $size, false).'</a>';
        $output .= "<{$itemtag} class='gallery-item'>";
        $output .= "
            <{$icontag} class='gallery-icon'>
                $link
            </{$icontag}>";

         if ( $captiontag && trim($attachment->post_title) ) {  //caption display
            $output .= "
                <{$captiontag} class='gallery-caption'>
                " .wp_get_attachment_link($id, $size, true, false, wptexturize($attachment->post_title))  . "
                </{$captiontag}>";
           }

        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= '<br style="clear: both" />';
    }
share|improve this question

2 Answers 2

up vote 0 down vote accepted
  1. As pointed in the other answer, you should not have prettyPhoto part

  2. The link should be changed to utilize the function get_attachment_link

Basically change this line in your code

$link = '<a href="'.get_attachment_link($id).'">'.wp_get_attachment_image($id, $size, false).'</a>';
share|improve this answer
    
Mridul it works like a charm, but it doesn't display image name and post title above image on attachment page eg: POSTABC--Image234 –  10wtaylor Dec 4 '12 at 12:34
    
The contents of the attachment page doesn't depend on this code. Please look for the appropriate file in your theme. This might help- codex.wordpress.org/images/1/18/Template_Hierarchy.png –  Mridul Aggarwal Dec 4 '12 at 12:50
    
ohk, thanks. I'm not able to find my theme file name in this png. –  10wtaylor Dec 4 '12 at 13:06

Looks like you should remove the prettyPhoto parts, such as

$i = 0;
$rand_id = mt_rand(1,1000);
foreach ( $attachments as $id => $attachment ) {
    $image_attributes = wp_get_attachment_image_src( $id,'large' );


    $link = isset($attr['link']) && 'file' == $attr['link'] ? '<a href="'.$image_attributes[0].'"  ]">'.wp_get_attachment_image($id, $size, false).'</a>' : '<a href="'.$image_attributes[0].'"  ]">'.wp_get_attachment_image($id, $size, false).'</a>';
    $output .= "<{$itemtag} class='gallery-item'>";
    $output .= "
        <{$icontag} class='gallery-icon'>
            $link
        </{$icontag}>";

     if ( $captiontag && trim($attachment->post_title) ) {  //caption display
        $output .= "
            <{$captiontag} class='gallery-caption'>
            " .wp_get_attachment_link($id, $size, true, false, wptexturize($attachment->post_title))  . "
            </{$captiontag}>";
       }

    $output .= "</{$itemtag}>";
    if ( $columns > 0 && ++$i % $columns == 0 )
        $output .= '<br style="clear: both" />';
}
share|improve this answer
    
It is still same! –  10wtaylor Dec 4 '12 at 11:20
    
Meaning the outcome is still the same, or that the code i gave you was 'the same'? –  Danny Michel Dec 4 '12 at 11:28
    
output is same ! –  10wtaylor Dec 4 '12 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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