Support

Account

Home Forums Add-ons Gallery Field How to get Gallery from a Custom Post Type?

Solved

How to get Gallery from a Custom Post Type?

  • Hello!

    I am trying to display a gallery from a custom post type.

    I created a new Custom Post Type using Custom Post Type UI plugin and called it ‘photo’.

    My ACF Custom Field is called ‘gallery’.

    This is a one pager website so I am trying to display the images from the index.php.

    I started with the following code which worked fine when the gallery was included on a regular Post page:

    <?php
    
    $images = get_field('gallery');
    
    if( $images ): ?>
    
            <?php foreach( $images as $image ): ?>
    
                         <div class="item" ><img src="<?php echo $image['sizes']['large']; ?>"  /></div>
    
            <?php endforeach; ?>
    
    <?php endif; ?>
    

    I am now trying to pull images form a custom post type like this:

    <?php
    
       $loop = new WP_Query( array( 'post_type' => 'photo') );
    	 while ( $loop->have_posts() ) : $loop->the_post();
    
    $images = get_field('gallery');
    
    if( $images ): ?>
    
            <?php foreach( $images as $image ): ?>
    
                         <div class="item" ><img src="<?php echo $image['sizes']['large']; ?>"  /></div>
    
            <?php endforeach; ?>
    
    <?php endif; ?>

    But this does not work- in fact, the whole page turns blank like when syntax is wrong.

    Any ideas how to fix this?

    Many thanks!

  • If you’re getting a blank page then you’re more than likely right and you have a syntax error in your code. Start by turning on debugging and debug display in WP to see what the error is https://codex.wordpress.org/Debugging_in_WordPress. Just looking at your code, if that’s the complete code, I’d say you’re missing endwhile; for your loop.

  • That was it! Thanks John!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to get Gallery from a Custom Post Type?’ is closed to new replies.