Support

Account

Home Forums Add-ons Gallery Field Include Gallery Field in Custom Post Type

Solving

Include Gallery Field in Custom Post Type

  • Just purchased the gallery plugin for ACF, but I’m not sure how to pull the gallery into the page as part of my custom post type. Below is my CPT markup – my gallery field is called ‘images’.

    *I really like the response soliloquy slider. If there is a way to use the gallery plugin with that – cool.

     <?php
    
    	 $loop = new WP_Query( array( 'post_type' => 'cars', 'posts_per_page' => 10 ) );
    	 while ( $loop->have_posts() ) : $loop->the_post();
    
    		 echo '<div class="products-container">';
    			 echo '<div class="products-image">';
    			 echo '<img src="';
    			 the_field('images');
    			 echo '" alt="';
    			 the_title();
    			 echo ' ';
    			 the_field('type');
    			 echo '"></a></div>';
    			 echo '<div class="products-text">';
    				 echo '<h2 class="products-title">';
    				 the_title();
    				 echo '</h2>';
    				 the_content();
    				 echo '<div class="products-link"><a href="';
    				 the_field('product_link');
    				 echo '" style="color:#dd6000;">';
    				 the_field('link_name');
    				 echo '</a></div></div>';
    
    			 echo '</div>';
    
    	   endwhile;
    
    	?>
  • Hi @theatereleven

    Before I review your code, can you please look over the documentation provided for the gallery field?

    http://www.advancedcustomfields.com/resources/field-types/gallery/

    Let me know if you have any questions regarding the example code.

    Thanks
    E

  • Elliot:

    I did try code from that page before posting the question. I’m a front end guy that dabbles with PHP just enough to do my WordPress sites. Your ACF has been a life saver for those like me.

    Anyway… if you can just show me how to include a gallery in a custom post type that would rock. For example, let’s just say the custom post type was only pulling in the ACF gallery (to make it easy) – below is how I’d pull in a normal ACF field in a CPT:

    <?php
    
    	 $loop = new WP_Query( array( 'post_type' => 'cars', 'posts_per_page' => 10 ) );
    	 while ( $loop->have_posts() ) : $loop->the_post();
    		echo '<img src="';
    		the_field('images');
                    echo '">';
    	   endwhile;
    
    	?>
  • Hi @theatereleven

    I have taken your code and inserted the code example from the gallery page. Hope this helps:

    
    <?php
    
    	 $loop = new WP_Query( array( 'post_type' => 'cars', 'posts_per_page' => 10 ) );
    	 while ( $loop->have_posts() ) : $loop->the_post();
    		
    		$images = get_field('gallery');
    		 
    		if( $images ): ?>
    		    <div id="slider" class="flexslider">
    		        <ul class="slides">
    		            <?php foreach( $images as $image ): ?>
    		                <li>
    		                    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    		                    <p><?php echo $image['caption']; ?></p>
    		                </li>
    		            <?php endforeach; ?>
    		        </ul>
    		    </div>
    		    <?php
    		
    		
    	   endwhile;
    
    	?>
    

    Thanks
    E

  • Thanks, tried that code but didn’t get anywhere. So forget my code.

    I dialed it back to this from the support document:

    <?php
     
    var_dump( get_field('car_photos') );
     
    ?>
    

    That returns a bool(false) on the page. Any ideas?

    Thanks for the help. I normally never have issues with ACF.

  • Hi @theatereleven

    First, check that the field name is correct. Even check your DB wp_postmeta table too.

    Next, check that ACF know which $post to load from. Edit your code and dump out get_the_ID()

    Is this the correct post_id to load from? Perhaps you have some code which has overridden the global $post object?

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

The topic ‘Include Gallery Field in Custom Post Type’ is closed to new replies.