Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStefaan Quackels
5,720 PointsGenesis Loop & Advanced Custom Fields Plugin
Maybe a more advanced question for this course: Using the Genesis functions, how can you create a loop to show just the images of a custom field['images'] for a new post_type['sponsor-images']?
The post type was made using the plugins Advanced Custom Fields & Custom Post Type UI.
1 Answer
Aaron Bolton
4,512 PointsThe code below should work.
Additional articles that may be helpful are: WP Codex Class Reference/WP Query: https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters Genesis Visual Hook Guide: https://genesistutorials.com/visual-hook-guide/ Advanced Custom Fields, Image Field Type Documentation: https://www.advancedcustomfields.com/resources/image/
<?php
//* Define a custom function that queries the database for your posts
function display_sponsor_post_images(){
//* Limit query to posts with "post type" set to "sponsor-images"
$queryArgs = array(
'post_type' => 'sponsor-images'
);
//* The query
$the_query = new WP_Query( $queryArgs );
//* The loop
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ) :
$the_query->the_post();
//* Get custom field "images" from current post
$image = get_field( 'images' );
//* Display the image
echo '<img src="'. $image[ "url" ] .'" alt="'. $image[ "alt" ] .'"/>';
endwhile;
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_sponsor_post_images' );
?>```