• Rather than my post thumbnails loading the single post on click, I wanted the image to load in a lightbox, I have the lightbox plugin installed.

    In my PHP template i just have the_post_thumbnail but in my functions file I have the following:

    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 300, 200 ); // 50 pixels wide by 50 pixels tall, resize mode
    // THIS LINKS THE THUMBNAIL TO THE POST PERMALINK
    
    add_filter( 'post_thumbnail_html', 'my_post_image_html', 300, 200 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    
    	$html = '<a href="#" rel="lightbox" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    
    	return $html;
    }

    As you can see I have amended the URL to remove the_permalink and insert the rel=lightbox attribute.

    This does bring up the lightbox but the image does not load, as if it cant find the path to the image.

    Could anyone help please?

Viewing 1 replies (of 1 total)
  • Thread Starter adejones

    (@adejones)

    I have found another post on this forum but their code also does not work for me, I amended my own to remove all above from functions.php and include the following in my template page:

    <?php $args = array(
            'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_status'    => 'inherit',
            'post_mime_type' => 'image',
    );?>
    
    <?php $attachments = new WP_Query( $args );
    $attachments->get_posts();
    
    $output = '';
    foreach( $attachments->posts as $attachment ) {
            $image_src = wp_get_attachment_image_src( $attachment->ID, 'large');
            $output = '<a href="'. $image_src[0] .'" rel="lightbox" class="hidden"></a>';
    }
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    ?>
    
    <a href="<?php $image_src[0]; ?>" rel="lightbox">
    <?php the_post_thumbnail( 'thumbnail-size'); ?>
    </a>
Viewing 1 replies (of 1 total)
  • The topic ‘Post Thumbnails to use lightbox’ is closed to new replies.