• Resolved mdopsch

    (@mdopsch)


    Hi, I still have some issues to display the gallery on the frontend.
    I use the twenty sixteen theme and I’ve tried to paste the following code under the excerpt of post.php:

    <?php $galleryArray = get_post_gallery_ids($post->ID); ?>
    <?php foreach ($galleryArray as $id) { 
    
        <img src="https://ww.wp.xz.cn/plugins/featured-galleries/&">
    
    <?php } ?>

    The foreach loop produces a syntax error.
    What am I doing wrong?
    Greets
    Martin

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Andy Mercer

    (@kelderic)

    A loop in PHP means that what is inside is going to be repeated over and over.

    So in your loop, what is inside is an image with a link back to my plugin page. Which isn’t a valid image link. That is what you are doing wrong. You need to use the $id inside the array, that is the ID of each image in the gallery.

    Use that ID to get the image URL, and plug that into your <img> tag.

    Thread Starter mdopsch

    (@mdopsch)

    Hi, ok i understand, thanks for your answer.

    Now I’ve tried it like this:

    <?php $galleryArray = get_post_gallery_ids($post->ID); ?>
    <?php foreach ($galleryArray as $id) { 
    
        <img src="<?php echo wp_get_attachment_url( $id ); ?>">
    
    <?php } ?>

    Still getting an error.

    Thread Starter mdopsch

    (@mdopsch)

    Okay; I’ve found the problem

    <?php $galleryArray = get_post_gallery_ids($post->ID); ?>
    <?php foreach ($galleryArray as $id) { ?>
    
        <img src="<?php echo wp_get_attachment_url( $id ); ?>">
    
    <?php } ?>

    Now the images appear.

    Thread Starter mdopsch

    (@mdopsch)

    Is there a way to show only the first image and link it to a lightbox gallery?
    I found one half working solution in you support forum:
    Show featured gallery in fancybox

    But this function is not recognized by wordpress:
    $attachment_meta = wp_get_attachment( $id );

    Thread Starter mdopsch

    (@mdopsch)

    So I added this to my content-single.php
    All the images in the gallery are displayed and linked.

    <?php $galleryarray = get_post_gallery_ids($post->ID);
    
    foreach ($galleryarray as $id) {
    $imagesize = wp_get_attachment_image_src( $id, 'thumb' );
     ?>
    
    <a href="<?php echo $imagesize[0]; ?>">
    <img src="<?php echo wp_get_attachment_url( $id ); ?>"></a>
    <?php } ?>

    But the (Huge IT) lightbox shows one image twice.
    I guess it has something to do with the
    $attachment_meta = wp_get_attachment( $id );

    Plugin Author Andy Mercer

    (@kelderic)

    wp_get_attachment_url Returns the full URL. wp_get_attachment_image_src Does gets the URL but lets you specify the size, rather than just the full size image.

    You probably want a thumbnail size for the <img> tag, and the full size for the .

    Thread Starter mdopsch

    (@mdopsch)

    Thanks Andy, though I’m not a developer just a designer it works for me now. And I even learned a bit more PHP. Good work, good support.

    Plugin Author Andy Mercer

    (@kelderic)

    I’m glad you got it working! 🙂

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

The topic ‘display galleries on frontend post.php’ is closed to new replies.