[Resolved] Any way at all of making the featured image a link to its media file in posts?

Home Forums Support [Resolved] Any way at all of making the featured image a link to its media file in posts?

Home Forums Support Any way at all of making the featured image a link to its media file in posts?

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1156495
    Michael

    So I’d like to show the featured image in a certain category of posts (elsewhere it is in the header). But ideally, due to the nature of the posts, I’d like users to be able to click the featured image so my lightbox will show it larger.

    Possible at all?

    #1156717
    David
    Staff
    Customer Support

    Hi there,

    try this PHP snippet:

    add_filter( 'post_thumbnail_html', 'db_post_thumb_media_link', 10, 3 );
     
    function db_post_thumb_media_link( $html, $post_id, $post_image_id ) {
    
        if ( !is_single() ) {
            // Return default if NOT single
            return $html;      
        } else {
            // Get URL of large image attachment
            $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
            // Wrap HTML in link
            $html = '<a href="' . $large_image_url[0] . '">' . $html . '</a>';
            return $html;
        }
    }
    #1157019
    Michael

    Thanks very much David, I didn’t think it was going to be possible.

    You guys rule!

    #1157534
    David
    Staff
    Customer Support

    Happy to be of help.

    #1926622
    Rafał

    It was very helpful for me, Thank You!
    I needed a caption as well.
    I used Block – Page Hero from Elements, and just put this shortcode block:
    [display_featured_image_with_caption] WITHOUT THE FILTER

    add_shortcode( 'display_featured_image_with_caption', function() {
        ob_start();
        $caption = get_post(get_post_thumbnail_id())->post_excerpt;
    	
        if ( has_post_thumbnail() ) {
            echo '<div class="post-hero grid-container"><figure>',
                '<a href="' . get_the_post_thumbnail_url(null, 'full') . '">',
                the_post_thumbnail('large'),
                '</a>';
            if ($caption) {
                echo '<figcaption>',
                    $caption,
                    '</figcaption>';
            }
            echo '</figure></div>';
        }
        return ob_get_clean();
    } );
    #1936320
    David
    Staff
    Customer Support

    Awesome – glad to hear that was of use, and thanks for sharing your version!!

    #1936327
    Michael

    I know it’s a bit cheeky, but I was after a way of doing this for images within a post, but without having to edit the post and make each image link to itself. I have dozens of posts where it’s just a straight image with no link, and a plugin that looks for images that are linked to themselves to display a lightbox. In all honesty, I can’t be bothered to edit all the posts, would a bit of code similar to the above be possible?

    #1936397
    David
    Staff
    Customer Support

    Hi Michael,

    Just to be clear – you would want any Image inside the post content to be ‘updated’ to include a link to its attachment file ?

    #1936415
    Michael

    That would be the aim, but the ability to use in an element so I could control where (categories etc) it was executed, would that be possible?

    #1936468
    David
    Staff
    Customer Support

    Hmmm… i don’t have a solution for that. Maybe some bright spark on stackexchange may have a solution.
    You would need to filter the_content of the post and replace any <img> elements with new html that includes an anchor link to the image attachment. Theres plenty of offerings to remove the attachment link just couldn’t find anything to add them in.

    #1936491
    Michael

    Hi David,

    No problems, it was a big ask. I’ll scour around see if I can find anything, or push comes to shove, just stop being lazy and edit all the posts manually.

    Appreciated.

    #1936515
    David
    Staff
    Customer Support

    I thought i had come across this before, so it may just take a little ( lot ) of googling.
    Let us know how you get on!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.