invp_get_the_photos( mixed $sizes, int|null $post_ID = null )

Fill arrays of thumb and large <img> elements and URLs to simplify the use of of vehicle photos.


Parameters Parameters

$sizes

(mixed) (Required)

$post_ID

(int|null) (Optional) The post ID of a vehicle. Must be passed when using this method outside the loop.

Default value: null


Top ↑

Return Return

(array) An array of thumbnail and full size HTML <img> elements plus URLs


Top ↑

Source Source

File: includes/template-tags.php

function invp_get_the_photos( $sizes, $post_ID = null ) {
	/**
	 * Backwards compatibility to versions before 5.4.0 where the
	 * incoming argument was a string not an array.
	 */
	if ( ! is_array( $sizes ) ) {
		$sizes = array( $sizes );
	}

	if ( empty( $post_ID ) ) {
		$post_ID = get_the_ID();
	}

	$cache_key_images = 'invp_get_the_photos_images_' . $post_ID;
	$images           = get_transient( $cache_key_images );
	if ( empty( $images ) ) {
		$images = get_posts(
			array(
				'meta_key'       => apply_filters( 'invp_prefix_meta_key', 'photo_number' ),
				'posts_per_page' => apply_filters( 'invp_query_limit', 1000, __FUNCTION__ ),
				'order'          => 'ASC',
				'orderby'        => 'meta_value_num',
				'post_mime_type' => 'image',
				'post_parent'    => $post_ID,
				'post_status'    => 'inherit',
				'post_type'      => 'attachment',
			)
		);
	}

	// Did we find any photos?
	if ( empty( $images ) ) {
		/**
		 * No. Perhaps this vehicle has attachments, but they don't have our
		 * meta key. Just rely on the post date for sequencing.
		 */
		$images = get_posts(
			array(
				'posts_per_page' => apply_filters( 'invp_query_limit', 1000, __FUNCTION__ ),
				'order'          => 'ASC',
				'orderby'        => 'post_date',
				'post_mime_type' => 'image',
				'post_parent'    => $post_ID,
				'post_status'    => 'inherit',
				'post_type'      => 'attachment',
			)
		);
	}

	if ( ! empty( $images ) ) {
		set_transient( $cache_key_images, $images, MINUTE_IN_SECONDS * 5 );
	}

	$cache_key_image_urls = 'invp_get_the_photos_image_urls' . implode( '_', $sizes ) . '_' . $post_ID;
	$image_urls           = get_transient( $cache_key_image_urls );
	if ( empty( $image_urls ) ) {
		$image_urls = array();
		foreach ( $images as $image ) {
			foreach ( $sizes as $size ) {
				$img_element = wp_get_attachment_image(
					$image->ID,
					$size,
					false,
					array( 'class' => "attachment-$size size-$size invp-image" )
				);

				$image_urls[ $size ][] = $img_element;

				if ( 'full' === $size ) {
					$image_urls['urls'][] = INVP::extract_image_element_src( $img_element );
				}
			}
			if ( empty( $image_urls['urls'] ) ) {
				$img_element          = wp_get_attachment_image(
					$image->ID,
					'full',
					false,
					array( 'class' => "attachment-$size size-$size invp-image" )
				);
				$image_urls['urls'][] = INVP::extract_image_element_src( $img_element );
			}
		}
		set_transient( $cache_key_image_urls, $image_urls, MINUTE_IN_SECONDS * 5 );
	}

	/**
	 * Backwards compatibility to versions before 5.4.0 where the
	 * incoming argument was a string not an array.
	 */
	if ( 1 === count( $sizes ) && 'large' !== $sizes[0] ) {
		return $image_urls[ $sizes[0] ];
	}

	return $image_urls;
}