0

Okay so I have a custom post type called Multimedia Gallery. It's built using a repeater field that can take video, image, link, iframe, etc.

I'm using this plugin to insert these custom posts into blog posts, basically allowing me to insert my MM gallery into blog posts using a shortcode.

Problem is... the If and While statements seem to need a post ID to actually return the correct info. But I'm not quite sure to make that dynamic.

Here's my code:

<?php

/*
  Template Name: Gallerys
 */


//Variables

$initialCardLoad = 5;
$loopLazyLoad = 0;
$gridPostID = get_the_ID();



?>

<section class="grid-content">
    <div class="gridGallery">


<?php
// check if the repeater field has rows of data
if( have_rows('card', 355) ):

    // loop through the rows of data
   while ( have_rows('card', 355) ) : the_row();
$loopLazyLoad++; // iterate on loop each time you loop through
       // display a sub field value inside a card

        ?>

        <?php
        //VARIABLES//
        $title =  get_sub_field('card_title');
        $childImage = get_sub_field('card_picture');
        $file = get_sub_field('card_video');
        $video = $file['url'];

        if($loopLazyLoad > $initialCardLoad):

            if( $childImage ): ?>
                <div class="grid-item">
                    <a data-fancybox="gallery" data-caption="<?php echo $title ?>" href="<?php
                echo $childImage['url'];//big one here ?>">
                        <img class="lazy" data-src="<?php echo $childImage['url']; ?>">
                     </a>
            <?php endif; ?>

            <?php if( $video ): ?>
                <div class="grid-item">
                    <video class="gallery-video lazy" loop autoplay muted>
                        <source src="<?php echo $video; ?>" type="video/mp4">
                    </video>
        <?php endif; ?>
            </div> <?php //end of grid item div

        else : // Note the combination of the words.
        if( $childImage ): ?>
            <div class="grid-item">
            <a data-fancybox="gallery" data-caption="<?php echo $title ?>" href="<?php
            echo $childImage['url'];//big one here ?>">
                <img class="" src="<?php echo $childImage['url']; ?>">
            </a>
        <?php endif; ?>

            <?php if( $video ): ?>
            <div class="grid-item">
            <video class="gallery-video" loop autoplay muted>
                <source src="<?php echo $video; ?>" type="video/mp4">
            </video>

        <?php endif; ?>
            </div> <!-- end of grid item div -->

           <?php endif; //end of initial card load of loop


endwhile; //end of while statement

The 355 is a manual insertion of the proper post ID for the purposes of testing if it would even work. It does! But how do I make it dynamic? When I try to use get_the_ID() it just gets the ID of the post I'm inserting into, rather than the one I'm inserting from.

2
  • get_the_id() will always return current post/page ID, one thing you can do is use custom field to store the value (355) in current post and call it dynamically in the template. Hope this helps – Piyush Rawat Sep 28 '17 at 5:56
  • Thanks @PiyushRawat I was thinking of trying something like that, but figured it would mean having to manually set the ID value in a custom field for every single gallery. If it works though, it works! – David A. French Sep 28 '17 at 19:22
1

From the plugin code I can see that it uses query_posts() to set up a new context for your template if you're using display="blah.php".

So you should just need to add the loop around your template:

<?php
/**
 * Template Name: Gallery
 */

while ( have_posts() ): the_post(); // <-- Important bit
    if( have_rows( 'card' ) ):
        while ( have_rows( 'card' ) ) : the_row();
            // Stuff
        endwhile;
    endif;
endwhile; // <-- Important bit
0

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.