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.

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: 4000x4000+ 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..

share|improve this question
    
Do images already exist at this size in your WP installation? Maybe it is defaulting to that size because the 150x150 size does not exist? You can use a plugin like Regenerate Thumbnails to create the sizes you need. If that's not the issue, then there is something else in your code (not posted) that is linking to the different sized image. If you view source on the page, what do the img tags and links look like? –  jjeaton Jul 27 '11 at 23:55
    
the 150 x 150 setting works. Wordpress takes the nearest size added by the Additional Image Sizes (zui) plugin and resizes all the thumbnails to the right proportions (variable width and 150px high). The wordpress default link for the above code is the full image - that is what I want to change so that it pulls my 'medium' image as the link which would be 850 pixels high; ie big enough for most screens and fast enough for less powerfull devices and internet connections.... –  Aces Jul 28 '11 at 0:30
    
Are you outputting these images and links in a template or are the links already contained within post/page content in WP and you want to somehow filter and edit them when the page is displayed? –  jjeaton Jul 28 '11 at 1:56
    
10 images uploaded by the image uploader saved with the post but not put on the page. page-no-widgets.php edited to add the above code after the_content (+li, div and code comments) and saved as page-gallery.php in my child of Hybrid theme. I am using 2 filters in functions.php to add class and rel values but I can't get the preg_replace or the str_replace techniques used to alter the image that wp_get_attachment_link links it's thumbnails to.... –  Aces Jul 28 '11 at 3:09

3 Answers 3

You can specify the size you want to link in the wp_get_attachment_link function, isn't the code you posted working?

Try:

$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 , 'medium', false ); 
    }
}
share|improve this answer
    
The code works in that it lays out thumbnails based on the size: array(150,150). It doesn't alter the default 'full' size of the images those thumbnails link to when clicked. The colorbox displays shrunk down full size images rather than the wordpress medium size images which would be better.... (N.B. false doesn't seem to make any difference....) –  Aces Jul 27 '11 at 21:36

I am not sure if you can filter the links within the content in PHP, but you could use jQuery to replace the href of each of the links, selecting them based on those rel and class values you mentioned.

share|improve this answer
    
That's a bit beyond my skills.... –  Aces Jul 28 '11 at 23:01
up vote 1 down vote accepted

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='{$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....

share|improve this answer

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.