How can I get a different image size, if all I have is the link?

For various reasons, I can’t use WordPress’ built-in Thumbnail functionality.

What I want to do instead is to use the first image in the post as the thumbnail.

Read More

Here’s what I found in the codex: Show the first image associated with the post.

However, the problem with that is that if the post has multiple images, but the first image in the post is not actually the one that was uploaded first, you’ll end up getting the second image instead of the first one.


So, I decided to use something similar to this approach, which uses a regex to parse the_content to find the first post.

This works out fine, but I end up with the image size that was used in the post, and I only want the Thumbnail size.


So, here’s the question: If I have a link to an image, is there any way I can get a different size?

Seems what I need is to somehow get the attachment ID, so that I can get the image size with this:

wp_get_attachment_link( $id, 'thumbnail' );

The problem is, how do I get an ID if all I have is the URL?

Related posts

Leave a Reply

3 comments

  1. I decided to use this, which is based on @AndresYanez’s answer:

    function get_image_id_by_link($link)
    {
        global $wpdb;
    
        $link = preg_replace('/-d+xd+(?=.(jpg|jpeg|png|gif)$)/i', '', $link);
    
        return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE BINARY guid='$link'");
    }
    

    This is much more succinct (since it doesn’t jump through the hoops of first removing the extension and then adding it back in), and is a little more accurate (since the . is escaped and the query is case sensitive).

  2. function get_attachment_id_from_src ($src) {
        global $wpdb;
    
        $reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";
    
        $src1 = preg_replace($reg,'',$src);
    
        if($src1 != $src){
            $ext = pathinfo($src, PATHINFO_EXTENSION);
            $src = $src1 . '.' .$ext;
        }
    
        $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
        $id = $wpdb->get_var($query);
    
        return $id;
    }
    

    Credits to pathorsley: http://www.pathorsley.com/code/get-the-wordpress-post-attachment-id-from-an-image-src/

  3. The Codex is a valid source

    Sometimes The Codex isn’t that wrong…

    Show attachments for the current post

    This is a slightly modified example from the Codex.

    <?php
    // Do this inside The Loop (where $post->ID is available).
    global $post;
    $args = array( 
         'post_type'    => 'attachment'
        ,'numberposts'  => 1
        ,'post_status'  => null
        ,'post_parent'  => $post->ID
        ,'orderby'      => 'ID'
        ,'order'        => 'ASC' 
    ); 
    $attachments = get_posts( $args );
    if ( $attachments ) 
    {
        foreach ( $attachments as $attachment ) 
        {
            echo apply_filters( 'the_title' , $attachment->post_title );
            the_attachment_link( $attachment->ID , false );
        }
    }
    ?>
    

    Be smart – use the system behind the system

    The changes to the Codex example are easy: The numberposts are set to 1, the orderby value is the ID and it’s sorting ASC to get the post with the lowest ID first.

    So here’s why this is smart: IDs are given one after each other, so the first uploaded post will have the lowest ID.

    In the example above, you can simply exchange the last function with wp_get_attachment_link() and save it in some var, that you can reuse later.