• Resolved Jon Hardison

    (@jonhardison)


    Hey:

    Your plug-in is working well for me but I do have one question:

    I have a custom post type where images are uploaded specifically for the creation of before & afters, so the image IDs are stored in custom fields.

    Normally the way we grab these us by doing something like
    <?php the_field('before_image'); ?>

    Additionally, we’re building this into a theme or template, so when we use your shortcode we’re doing something like
    <?php echo do_shortcode( '[twenty20 img1="#" img2="#" direction="horizontal" offset="0.5" align="right" width="100%" before="Before" after="After" hover="false"]' ); ?>

    So the question is how might I go about populating the img1 and img2 values with data from custom fields? Can you recommend a way to get this done?

    Your time and consideration is much appreciated. ??

    • This topic was modified 5 years, 2 months ago by Jon Hardison.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Zayed Baloch

    (@zayedbaloch)

    Hi,

    Try the following code in your theme .php file.

    <?php 
      $imgBefore = get_field('before_image'); 
      $imgAfter = get_field('after_image'); 
    
      echo do_shortcode( '[twenty20 img1="' . $imgBefore . '" img2="' . $imgAfter . '" direction="horizontal" offset="0.5" align="right" width="100%" before="Before" after="After" hover="false"]' );
    ?>

    Br,

    Thread Starter Jon Hardison

    (@jonhardison)

    Nearly perfect! Thank you so much!

    Something about this is making it only work once per page.
    Is that just the $imgBefore needing to be changed for each instance?

    Thread Starter Jon Hardison

    (@jonhardison)

    Hmmm. My guess was wrong. LOL!
    The first instance works perfectly. I have a total of 5 instances and none will load beyond the first. And the console isn’t throwing any errors or anything.

    Plugin Author Zayed Baloch

    (@zayedbaloch)

    How many instances are you using in the post/page? If you need more than one then use the ACF Repeater field.

    Create the Repeater field with two images sub-fields. Then use the following code.

    <?php if( have_rows('twenty20_repeater') ): ?>
    
      while( have_rows('twenty20_repeater') ): the_row();
    
        $beforeImage = get_sub_field('before_image');
        $afterImage = get_sub_field('after_image');
    
      ?>
    
        <div>
    
          <?php echo do_shortcode( '[twenty20 img1="' . $beforeImage . '" img2="' . $afterImage . '" direction="horizontal" offset="0.5" align="right" width="100%" before="Before" after="After" hover="false"]' ); ?>
    
        </div>
    
      <?php endwhile; ?>
    
    <?php endif; ?>
    Thread Starter Jon Hardison

    (@jonhardison)

    My fault! The first field set was storing IDs where the subsequent fields were storing URLs.
    I’ve fixed it and it’s working and you’re awesome and I thank you VERY VERY MUCH!!!

    Plugin Author Zayed Baloch

    (@zayedbaloch)

    You are welcome. ??

    Hi Zayed,

    Thank you for making such a great plugin! I’m trying to do something similar to the surface comparison here: https://www.originalphotopaper.com/products/photo-papers/. Is there an easy way to “populat[e] the img1 and img2 values with data” using dropdown(s)?

    All the best,

    Mike

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting Image ID from custom field values’ is closed to new replies.