0

So the goal is to have a href to the full size image and the img src to be the thumbnail.

Up till no i have this

 <?php $gallery = get_post_gallery_images();
    foreach($gallery as $img){ ?>
        <div class="galerija-box">
            <a href="<?php echo $img ?>" rel="lightbox"><img src="<?php echo $img ?>"/></a>
        </div>
    <?php } ?>

This only gives me the image sizes the user selects when inserting the gallery into the post.

Any ideas How I could have different urls for the a href and img src?

Thanks.

0

There might be other possibilities to this, mine is given below ..

  1. First you use the get_post_gallery function to store the image id's in a separate array ..

    if ( get_post_gallery() ) {
    
        $gallery        = get_post_gallery( get_the_ID(), false );
        $galleryIDS[]   = array();
    
        /* Loop through all the image and store them one by one */
        foreach( $gallery['ids'] as $ids ) {
    
            $galleryIDS[] = $ids;
    
        }
    }
    
  2. Then you output each image by id using wp_get_attachment_image as shown below ..

    <?php foreach ($galleryIDS as $key => $value ) { ?>
    
        <div class="galerija-box">
                <a href="<?php echo wp_get_attachment_image( $value, 'medium' ) ?>"  rel="lightbox">
                    <img src="<?php echo wp_get_attachment_image( $value, 'full' ) ?>"/>
                </a>
        </div>
    <?php } ?>
    

I didn't test the code so it might have some syntax issues, but the method should work for you ..

EDIT

As mentioned before I didn't check the code if its functional or not. So I tested it out, found a few bugs, fixed them. Below is the working code ..

if ( get_post_gallery() ) {

    $gallery        = get_post_gallery( get_the_ID(), false );
    $galleryIDS     = $gallery['ids'];
    $pieces         = explode(",", $galleryIDS);

    foreach ($pieces as $key => $value ) { 

        $image_medium   = wp_get_attachment_image_src( $value, 'medium'); 
        $image_full     = wp_get_attachment_image_src( $value, 'full'); 
    ?>


    <div class="galerija-box">
            <a href="<?php echo $image_medium[0] ?>"  rel="lightbox">
                <img src="<?php echo $image_full[0] ?>"/>
            </a>
    </div>
    <?php 
    }
}
?>

Reference: https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

7
  • this looks like a lot of sense :), I mean yea will try it and let you know, but i think this is what I need. Thanks. Jan 4 '16 at 11:36
  • Don't forget to mark the correct answer to close this thread when you test it out. Thanks .. Jan 4 '16 at 11:38
  • var_dump($galleryIDS); returns zero results... :) Jan 4 '16 at 12:29
  • Edited the answer .. Jan 4 '16 at 16:53
  • You're calling get_post_gallery() twice, I'm sure you can modify it so it's only called once ;-)
    – birgire
    Jan 4 '16 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.