Community Forums › Forums › Archived Forums › General Discussion › Genesis Loop for Custom Post Type Images
- This topic has 6 replies, 2 voices, and was last updated 7 years, 10 months ago by Stefaan5.
-
AuthorPosts
-
June 10, 2016 at 6:52 am #187325Stefaan5Member
Hello,
Anyone know how to create a loop to show just the images of a post_type?
The post type was made using te plugins Advenced Custom Fields & Custom Post Type UI.I thought this would've been enough, but it deffinetely didn't work :S:
add_action( 'genesis_after_entry', 'custom_loop' );
function custom_loop() {
$args = ( array(
'post_type' => 'sponsor-images',
'post_status' => 'publish',
'order' => 'asc'
) );genesis_custom_loop( $args );
wp_reset_query();
}P.S.: I'm new to Genesis, if anyone could point me to the right direction of finding some useful tutorials for Genesis functions that would be awesome.
Thanks!
June 10, 2016 at 1:21 pm #187341Victor FontModeratorTry this. It will get you started: http://victorfont.com/add-pages-cpts-wordpress-query/
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?June 13, 2016 at 5:46 am #187469Stefaan5MemberThanks for the help Victor.
I've been trying to figure this out for days but I'm not sure what to do next though.
I can't get the values to loop.
I'm trying to output the images from the field 'sponsor-image' from the custom post type 'sponsor-images'. This in the genesis_entry_footer location.Do I use the WP_query on the front-page? Which arguments should I use then?
Regards,
Stefaan
June 13, 2016 at 9:55 am #187480Victor FontModeratorThe code you have above will never generate a loop. A loop is specifically for displaying posts on a page and modifications to the loop are made in the pre_get_posts filter. If I'm understanding what you want to do, you've created a custom post type and added ACF fields to it.
- Do the ACF fields contain links to the sponsor images?
- How many sponsor images are attached to each single CPT? Is it variable number or is there a 1-1 relationship?
- How many images do you want to display in each post's Entry Footer?
Displaying ACF content in a widget area is a whole other ballgame and nothing like your trying to do with your code.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?June 14, 2016 at 1:25 am #187508Stefaan5MemberThat's right, I've created a CPT through CPT UI-plugin and a field through ACF-plugin.
These do not have to be placed in a widget.1. The CPT 'sponsor-images' has a field 'sponsor-image'. Which has a value of one image.
I want to be able to add sponsor images and remove them through the WP-CMS in the CPT 'sponsor-images'. The images do not need a link.2. The CPT has 8 sponsor images for the moment. But I want to be able to remove and add to this through the CMS.
The field contains only 1 image per sponsor.3.This only has to be displayed on the front-page.php, preferably in the location genesis_after_entry.
The amount of images that should be displayed are the amount of sponsors created in the CMS.I've gotten it to half-work with a forreach loop with the following code I got from the WP-forums.
However, when I add more sponsors, the new images aren't displayed anymore.
=============================================================================================
add_action( 'genesis_after_entry', 'image_looper' );function image_looper(){
$post_type = 'sponsor-images';
global $wpdb;
$where = get_posts_by_author_sql( $post_type );
$query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%') AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts ) ORDER BY p.post_date DESC";
$results = $wpdb->get_results( $query );if ( $results ) {
foreach ( (array) $results as $image ) {
$url = get_attachment_link($image->post_parent);
$thumb = wp_get_attachment_thumb_url( $image->ID );
$alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
echo '<li class="SponsorImg">';
}
}
}
=============================================================================================June 17, 2016 at 2:56 am #187727Stefaan5MemberI think I made this more complex than I should have.
I was able to solve this issue through a different approach, using a simple foreach loop with a conditional.
This is the code://Add Sponsors through foreach-loop
add_action( 'genesis_after_entry', 'add_img_loop');function add_img_loop(){
echo '<ul id="sponsorimg-container">';
$sponsorimg1 = get_field('sponsor_1');
$sponsorimg2 = get_field('sponsor_2');
$sponsorimg3 = get_field('sponsor_3');
$sponsorimg4 = get_field('sponsor_4');
$sponsorimg5 = get_field('sponsor_5');
$sponsorimg6 = get_field('sponsor_6');
$sponsorimg7 = get_field('sponsor_7');
$sponsorimg8 = get_field('sponsor_8');
$sponsorimgarr = array( $sponsorimg1, $sponsorimg2, $sponsorimg3, $sponsorimg4, $sponsorimg5, $sponsorimg6, $sponsorimg7, $sponsorimg8);foreach ( $sponsorimgarr as $sponsorimage){
if( !empty ($sponsorimage) ){
echo '<li class="SponsorImg">';
}
}
echo '';
}June 22, 2016 at 7:10 am #188014Stefaan5MemberBesides using the foreach loop, the following worked for a new Post type using the WP_query.
I was looking to create images with a url going to their respective pages.
Meaning I had had to create a post-type with the title, featured image and an advanced custom field for the custom url.//Create loop for custom post type 'book'
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action('genesis_loop', 'CPT_loop');function CPT_loop(){
$args = array(
'posts_per_page' => -1,
'post_type' => 'books',
'order' => 'ASC',
'order_by' => 'menu_order',
'paged' => $paged
);$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
echo '<ul id="book-grid">';
while ( $loop->have_posts() ) : $loop->the_post();
?> <li class="bookImg" style="background-image: url(<?php the_post_thumbnail_url(); ?>);">" class="overlay"> <h2><?php the_title() ?></h2>
<?php endwhile;
echo '';
do_action( 'genesis_after_endwhile' );
endif;wp_reset_postdata();
} -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.