From c06a7d44cf72acc9c54922a59f6a47b8a8bf4d5f Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 12 Jan 2023 22:21:48 +0100 Subject: [PATCH] re-added max_images check props @mexon --- includes/model/class-post.php | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 74b2469..512a003 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -88,26 +88,30 @@ class Post { $id = $this->post->ID; $image_ids = array(); + // list post thumbnail first if this post has one if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) { $image_ids[] = \get_post_thumbnail_id( $id ); $max_images--; } - // then list any image attachments - $query = new \WP_Query( - array( - 'post_parent' => $id, - 'post_status' => 'inherit', - 'post_type' => 'attachment', - 'post_mime_type' => 'image', - 'order' => 'ASC', - 'orderby' => 'menu_order ID', - 'posts_per_page' => $max_images, - ) - ); - foreach ( $query->get_posts() as $attachment ) { - if ( ! \in_array( $attachment->ID, $image_ids, true ) ) { - $image_ids[] = $attachment->ID; + + if ( $max_images > 0 ) { + // then list any image attachments + $query = new \WP_Query( + array( + 'post_parent' => $id, + 'post_status' => 'inherit', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + 'order' => 'ASC', + 'orderby' => 'menu_order ID', + 'posts_per_page' => $max_images, + ) + ); + foreach ( $query->get_posts() as $attachment ) { + if ( ! \in_array( $attachment->ID, $image_ids, true ) ) { + $image_ids[] = $attachment->ID; + } } }