Can anyone help me filter wp_get_attachment_link so that a particular occurence of it links to the ‘medium’ or other size image rather than the full size.
I have the following in a page template:

$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
    echo wp_get_attachment_link( $attachment->ID , array(150,150) ); 
    }
}

I can add a filter to add class or rel but I can’t find anyway to alter the default (as originally uploaded) full size image linked to in the template ….
The above works fine with colorbox (not plugin) to create a lightbox, but if a user uploads a very large image (ie: 4000×4000+ pixels), the link will load too slowly and I don’t want the public to be able to download a print quality image from the lightbox..

3 Answers
3

I think I’ve answered my own question sort of….

As I was using a child theme of Hybrid I activated the cleaner gallery extension in the functions.php file: add_theme_support( 'cleaner-gallery' );

Then, based on the topic here I created my own filter:

add_filter( 'cleaner_gallery_image', 'my_gallery_image', 10, 4 );
function my_gallery_image( $image, $id, $attr, $instance ) {

        $post = get_post( $id );
        $image_src = wp_get_attachment_image_src( $post->ID, 'medium' );
        $image_thumb = wp_get_attachment_image_src( $post->ID, 'Custom Thumb' );
        $title = esc_attr( $post->post_title );

        $image = "<a href="https://wordpress.stackexchange.com/questions/24260/{$image_src[0]}"><img src="{$image_thumb[0]}" border="0"></a>";

    return $image;
}

There are still things that are not right such as the title, but it answers the original question, though I’m sure it could be improved on as I’m rather going by the seat of my pants….

Leave a Reply

Your email address will not be published. Required fields are marked *