• Resolved mtm

    (@flatpack-music)


    I am trying to create a lightbox to display the post thumbnail/featured image in a custom post type (the page link given is an example, the image in question is the first on the page). Using the following code I’m creating an attachment link which opens as you’d expect without the lightbox:

    <?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>	
    <a href="<?php echo get_attachment_link( get_post_thumbnail_id() ); ?>"><?php the_post_thumbnail( 'Feed' ); ?></a>
    <?php } ?>

    I’m aware that SLB may require me to activate manually as per this article but despite managing to get this working on the gallery (lower down the page) I haven’t found a way to make this work here.

    I’ll illustrate my logic, although it’s clearly wrong (this simply breaks the page):

    <?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>	
     <?php $content = '<a href="<?php echo get_attachment_link( get_post_thumbnail_id() ); ?>"><?php the_post_thumbnail( 'Feed' ); ?></a>';
     if ( function_exists('slb_activate') ) {
      $content = slb_activate($content);
     }
     echo $content; ?>
    <?php } ?>

    What should I be doing differently to get the full size featured image to open into a lightbox?

    • This topic was modified 7 years, 4 months ago by mtm.
    • This topic was modified 7 years, 4 months ago by mtm.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Archetyped

    (@archetyped)

    Hi, it looks like your code has nested <?php tags, which results in an error. A PHP block (started by <?php) must be closed with ?> before another PHP block is started.

    This can get messy quickly, so I would recommend reevaluating your code to eliminate redundant separate PHP blocks where possible. For example, since you are creating and echoing a variable (rather than outputting it directly to the browser), the entire block of code in your second example could be enclosed in a single PHP block by using string concatenation to build the link.

    Also, enabling WP_Debug during testing can help in future situations by displaying warning/error messages when something isn’t working as expected.

    Please post your updated code if you are are still experiencing issues once you’ve made the necessary changes and I would be glad to take another look.

    Thread Starter mtm

    (@flatpack-music)

    Thanks for replying – I finally got there using this:

    <?php if ( get_the_post_thumbnail() != '' ) { ?>
     <div class="narrowhalf">
      <div class="featimgwrap">
       <?php 
        $id = get_post_thumbnail_id();
        $link = wp_get_attachment_link( $id, 'Feed' );
        $content = $link;
       ?>	
       <?php $content = slb_activate($content);
       }
       echo $content; ?>
      </div>
     </div>
    <?php ?>

    I guess the nested php block rule includes instructions included in quotes (“”) – everything needs to be turned into a variable first.

    Thanks for your help.

    Plugin Author Archetyped

    (@archetyped)

    Awesome!

    A few notes:

    1. You can use has_post_thumbnail() instead of get_the_post_thumbnail() in the initial if statement for a cleaner conditional.

    2. The closing/opening ?><?php tags between the 2 $content variable assignments are unnecessary, as you’re still executing PHP code.

    3. Enclosing the slb_activate() call in if ( function_exists('slb_activate') ) ... is highly recommended to avoid any errors if SLB is temporarily deactivated, etc.

    4. The closing bracket for the if statement should come after the final closing </div> tag to match the positioning of the opening bracket of the statement (which is before the first <div> tag). Otherwise, if a post does not have a thumbnail, you’ll get an error for outputting a non-existent $content variable, as well as some browser rendering issues due to outputting </div> tags without matching <div> tags.

    Thread Starter mtm

    (@flatpack-music)

    Thanks so much – I really do appreciate the time you’ve spent helping me understand and improve this. For the record (and in case anyone ends up here trying to answer the same problem), here’s the final code then:

    <?php if (has_post_thumbnail()) { ?>
     <div class="narrowhalf">
      <div class="featimgwrap">
       <?php 
       $id = get_post_thumbnail_id();
       $link = wp_get_attachment_link( $id, 'Feed' );
       $content = $link;
       $content = slb_activate($content);
       if (function_exists('slb_activate')) {
        $content = slb_activate($content);
       }
       echo $content; ?>
      </div>
     </div>
    <?php } ?>

    Working very well, thanks.

    Plugin Author Archetyped

    (@archetyped)

    Looks great, just remove the extra $content = slb_activate($content); and you’re golden!

    Thread Starter mtm

    (@flatpack-music)

    Oh dammit. Yes – thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Post Thumbnail Lightbox’ is closed to new replies.