Home Forums Add-ons Gallery Field Relationship and Gallery


Relationship and Gallery

  • Hi there, great plugin, I use it all the time. I also bought the Gallery plugin, works like a charm!

    Question: I have two custom post types, for a client that manufactures custom products. The CPT’s are “design” and “designer”. I add a post for the designer. Then I add a post for the design, and use the relationship field (“designer_name”) to assign the designer of that design (and show a thumbnail and a link to their full profile). It was working perfectly when I was using the built-in Featured Image, like so:

    $args = array(
    'numberposts' => -1,
    'post_type' => 'design',
    'meta_query' => array(
    	'key' => 'designer_name',
    	'value' => $post->ID,
    	'compare' => 'LIKE',
    ) );
    $posts_array = get_posts( $args );
    if( $posts_array ) {
    foreach( $posts_array as $related ) {
    	echo get_the_post_thumbnail($related->ID, 'thumbnail');
    	echo '<a href="' . $related->guid . '">' . $related->post_title . '</a>';

    This works great, but now I’m using the Gallery plugin, because I have more than one image for each designer/design. But on this particular area, I want to ONLY get the first image from the gallery, like so:

    <?php $gallery_images = get_field('gallery'); ?> 
    <img src="<?php echo $gallery_images[0]['url']; ?>" />

    This works fine by itself, elsewhere on the site, but how would I combine the first bit of code with the second: basically how do I replace the get_the_post_thumbnail with this gallery code above?


  • Hi @miketargett

    No worries. All you need to do is use the second parameter $post_id to target the post that you want to load the gallery data from.

    Your code would look like this:

    foreach( $posts_array as $related ) {
    	$gallery_images = get_field('gallery'); 
    	echo '<img src="' . $gallery_images[0]['url'] . '" />';
    	echo '<a href="' . $related->guid . '">' . $related->post_title . '</a>';


  • OH man, so simple, thanks so much, and for the quick reply! Cheers, -Mike

  • This reply has been marked as private.
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Relationship and Gallery’ is closed to new replies.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.