[Support request] Clickable featured image on single post/page

Home Forums Support Clickable featured image on single post/page

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1333738
    Bas

    Perhaps a stupid question about something I believe is a standard feature on other themes, but I couldn’t find it in GP:

    How can I make the featured images on the single posts and pages ‘clickable’ and linking to the original image?

    I’m using the Photoswipe lightbox.

    #1333834
    David
    Staff
    Customer Support

    Hi there,

    try this PHP Snippet:

    add_filter( 'generate_single_featured_image_html', 'custom_add_post_thumbnail_link',25,1 );
    function custom_add_post_thumbnail_link($image_html) {
        if( $image_html == '' ) {
            $image_html = $image_html;
        } else {
            $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
            $linked_html = '<a href="' . $large_image_url[0] . '">' . $image_html . '</a>';
        }
        return $linked_html;
    }

    It will add a link to the Large image.

    #1333869
    Bas

    Thanks! How can it link to the full-sized image? We’re very consistent about images sizes, they’re all maximum 1920 px wide.

    #1333887
    David
    Staff
    Customer Support

    In this part of the code:

    wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');

    change large to full so it becomes:

    wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');

    #1333945
    Bas

    Great!

    For the people using the Photoswipe lightbox, this lightbox doesn’t work when there’s only one image in the post/image. In that case use the following code:

    
    add_filter( 'generate_single_featured_image_html', 'custom_add_post_thumbnail_link',25,1 );
    function custom_add_post_thumbnail_link($image_html) {
        if( $image_html == '' ) {
            $image_html = $image_html;
        } else {
            $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
            $linked_html = '<a href="' . $large_image_url[0] . '" data-lbwps-gid=" $large_image_url[0] " >' . $image_html . '</a>';
        }
        return $linked_html;
    }
    #1334017
    David
    Staff
    Customer Support

    You’re welcome and thanks for sharing your final method.

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