Support

Account

Home Forums Add-ons Gallery Field Display latest gallery images on the homepage

Solving

Display latest gallery images on the homepage

  • Is there a way to display the the most recent images added to any gallery field on the site on the homepage?

    If I had for example an “Artist” post type and within that post type was a gallery custom field to add a portfolio for that artists work.
    If I had 100’s of artists and I wanted to just display a collection of the latest uploaded images to artist’s galleries on the homepage, how would I do it?

  • Hi @mfteam

    You can use a WP function called get_posts or WP_Query to query the database. There is a documentation article over on the docs page to get you started with this.

    You can query the DB for artist posts ordered by ‘modified’ date, and limited to x amount.

    Then you can loop through those artists and load the gallery field data, and then do what ever you wish with teh data such as displaying the first image and a link to the artists permalink.

    Does that help?

    Thanks
    E

  • I have this exact same request. I have setup the following with wp_query and it does display the artist’s gallery images. However, it does not sort them by the gallery custom field across all posts. Instead it sorts them by the CPT post first and then sorts the photos in the order you have them in the gallery field.

    //* Define a custom function that queries the database for your posts
    function display_artist_post_images(){
    
    		//* Limit query to posts with "post type" set to "artist"
    		$queryArgs = array(
    			'post_type' => 'artist',
    			'orderby' => 'date',
    			'order' => 'ASC'
    		);
    
    		//* The query
    		$the_query = new WP_Query( $queryArgs );
    
    		//* The loop
    		if( $the_query->have_posts() ) :
    			echo '<ul>';
    			while( $the_query->have_posts() ) :
    				$the_query->the_post();
    				$images = get_field('gallery');
    				if( $images ):
    						foreach( $images as $image ):
    							echo '<li>';
    								echo '<a href=';
    								echo $image['url'];
    								echo '>';
    									echo '<img src=';
    									echo $image['sizes']['thumbnail'];
    									echo ' alt=';
    									echo $image['alt'];
    									echo ' />';
    								echo '</a>';
    								echo '<p>';
    								echo $image['caption'];
    								echo '</p>';
    							echo '</li>';
    						endforeach;
    				endif;
    			endwhile;
    			echo '</ul>';
    		endif;
    
    		//* Restore original Post Data
    		wp_reset_postdata();
    	}
    	//* Add your custom function to the site using WP's "add_action" function with a Genesis hook.
    	add_action( 'genesis_entry_content', 'display_artist_post_images' );
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display latest gallery images on the homepage’ is closed to new replies.