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
Return Return
(array) An array of thumbnail and full size HTML <img> elements plus URLs
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 ( ! in_array( 'full', $sizes, true ) ) {
$sizes[] = 'full';
}
if ( empty( $post_ID ) ) {
$post_ID = get_the_ID();
}
// Use the object cache instead of transients to avoid MySQL deadlocks when
// concurrent requests for the same vehicle all miss the cache simultaneously
// and race to write the same options table row.
$cache_group = 'invp_photos';
$cache_key_images = 'images_' . $post_ID;
$images = wp_cache_get( $cache_key_images, $cache_group );
if ( false === $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 ) ) {
wp_cache_set( $cache_key_images, $images, $cache_group, MINUTE_IN_SECONDS * 5 );
}
$cache_key_image_urls = 'image_urls_' . $post_ID;
$image_urls = wp_cache_get( $cache_key_image_urls, $cache_group );
if ( false === $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" )
);
if ( '' === $img_element ) {
continue;
}
$image_urls[ $size ][] = $img_element;
if ( 'full' === $size ) {
$image_urls['urls'][] = INVP::extract_image_element_src( $img_element );
}
}
}
wp_cache_set( $cache_key_image_urls, $image_urls, $cache_group, MINUTE_IN_SECONDS * 5 );
}
return $image_urls;
}
Expand full source codeCollapse full source codeView on Github