3

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..

4
  • 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
    Commented Jul 27, 2011 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
    Commented Jul 28, 2011 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
    Commented Jul 28, 2011 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
    Commented Jul 28, 2011 at 3:09

3 Answers 3

1

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....

0

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 ); 
    }
}
1
  • 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
    Commented Jul 27, 2011 at 21:36
0

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.

1
  • That's a bit beyond my skills....
    – Aces
    Commented Jul 28, 2011 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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