function get_the_image($args = array()) { global $post; $defaults = array('custom_key' => array('Thumbnail', 'thumbnail'), 'post_id' => $post->ID, 'attachment' => true, 'the_post_thumbnail' => true, 'default_size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'echo' => true); $args = apply_filters('get_the_image_args', $args); $args = wp_parse_args($args, $defaults); extract($args); if ($custom_key) { $image = image_by_custom_field($args); } if (!$image && $the_post_thumbnail) { $image = image_by_the_post_thumbnail($args); } if (!$image && $attachment) { $image = image_by_attachment($args); } if (!$image && $image_scan) { $image = image_by_scan($args); } if (!$image && $default_image) { $image = image_by_default($args); } if ($image) { $image = display_the_image($args, $image); } $image = apply_filters('get_the_image', $image); if ($echo && 'array' !== $format) { echo $image; } else { return $image; } }
/** * This is a highly intuitive script file that gets images * It first calls for custom field keys * If no custom field key is set, check for images "attached" to post * Check for image order if looking for attached images * Scan the post for images if $image_scan = true * Check for default image if there is one set * * Entirely rewrote the system in 0.2.3 * * @package Hybrid * @subpackage Media * * @since 0.1 */ function get_the_image($args = array()) { $defaults = array('custom_key' => array('Thumbnail', 'thumbnail'), 'post_id' => false, 'default_size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 0, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'show_caption' => false, 'width' => false, 'height' => false, 'echo' => true); $args = wp_parse_args($args, $defaults); extract($args); if (!is_array($custom_key)) { $custom_key = str_replace(' ', '', $custom_key); $custom_key = str_replace(array('+'), ',', $custom_key); $custom_key = explode(',', $custom_key); $args['custom_key'] = $custom_key; } $image = image_by_custom_field($args); if (!$image) { $image = image_by_attachment($args); } if (!$image && $image_scan) { $image = image_by_scan($args); } if (!$image && $default_image) { $image = image_by_default($args); } if ($image) { $image = display_the_image($args, $image); } else { $image = '<!-- No images were added to this post. -->'; } if ($echo) { echo $image; } else { return $image; } }
/** * This is a highly intuitive function that gets images. It first calls for custom field keys. If no * custom field key is set, check for the_post_thumbnail(). If no post image, check for images * attached to post. Check for image order if looking for attached images. Scan the post for * images if $image_scan = true. Check for default image if $default_image = true. If an image * is found, call display_the_image() to format it. * * @since 0.1 * @global $post The current post's DB object. * @param array $args Parameters for what image to get. * @return string|array The HTML for the image. | Image attributes in an array. */ function get_the_image($args = array()) { global $post; /* Set the default arguments. */ $defaults = array('custom_key' => array('Thumbnail', 'thumbnail'), 'post_id' => $post->ID, 'attachment' => true, 'the_post_thumbnail' => true, 'default_size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'echo' => true); /* Allow plugins/themes to filter the arguments. */ $args = apply_filters('get_the_image_args', $args); /* Merge the input arguments and the defaults. */ $args = wp_parse_args($args, $defaults); /* Extract the array to allow easy use of variables. */ extract($args); /* If a custom field key (array) is defined, check for images by custom field. */ if ($custom_key) { $image = image_by_custom_field($args); } /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */ if (!$image && $the_post_thumbnail) { $image = image_by_the_post_thumbnail($args); } /* If no image found and $attachment is set to true, check for an image by attachment. */ if (!$image && $attachment) { $image = image_by_attachment($args); } /* If no image found and $image_scan is set to true, scan the post for images. */ if (!$image && $image_scan) { $image = image_by_scan($args); } /* If no image found and a $default_image is set, get the default image. */ if (!$image && $default_image) { $image = image_by_default($args); } /* If an image is returned, run it through the display function. */ if ($image) { $image = display_the_image($args, $image); } /* Allow plugins/theme to override the final output. */ $image = apply_filters('get_the_image', $image); /* Display the image if $echo is set to true and the $format isn't an array. Else, return the image. */ if ($echo && 'array' !== $format) { echo $image; } else { return $image; } }
function igr_get_gallery_images($args = array()) { $defaults = array('custom_key' => array('Thumbnail', 'thumbnail'), 'post_id' => false, 'attachment' => true, 'default_size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'echo' => true); $args = apply_filters('igr_get_gallery_images_args', $args); $args = wp_parse_args($args, $defaults); extract($args); if (!is_array($custom_key)) { $custom_key = str_replace(' ', '', $custom_key); $custom_key = str_replace(array('+'), ',', $custom_key); $custom_key = explode(',', $custom_key); $args['custom_key'] = $custom_key; } if ($custom_key && $custom_key !== 'false' && $custom_key !== '0') { $image = image_by_custom_field($args); } if (!$image && $attachment && $attachment !== 'false' && $attachment !== '0') { $image = image_by_attachment($args); } if (!$image && $image_scan) { $image = image_by_scan($args); } if (!$image && $default_image) { $image = image_by_default($args); } if ($image) { $image = display_the_image($args, $image); } $image = apply_filters('igr_get_gallery_images', $image); if ($echo && $echo !== 'false' && $echo !== '0' && $format !== 'array') { echo $image; } else { return $image; } }
/** * This is a highly intuitive function that gets images. It first calls for custom field keys. If no * custom field key is set, check for the_post_thumbnail(). If no post image, check for images * attached to post. Check for image order if looking for attached images. Scan the post for * images if $image_scan = true. Check for default image if $default_image = true. If an image * is found, call display_the_image() to format it. * * @since 0.1 * @global $post The current post's DB object. * @param array $args Parameters for what image to get. * @return string|array The HTML for the image. | Image attributes in an array. */ function get_the_image( $args = array() ) { global $post; /* Set the default arguments. */ $defaults = array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'post_id' => $post->ID, 'attachment' => true, 'the_post_thumbnail' => true, // WP 2.9+ image function 'default_size' => false, // Deprecated 0.5 in favor of $size 'size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'echo' => true ); /* Allow plugins/themes to filter the arguments. */ $args = apply_filters( 'get_the_image_args', $args ); /* Merge the input arguments and the defaults. */ $args = wp_parse_args( $args, $defaults ); /* If $default_size is given, overwrite $size. */ if ( !empty( $args['default_size'] ) ) $args['size'] = $args['default_size']; /* If $format is set to 'array', don't link to the post. */ if ( 'array' == $args['format'] ) $args['link_to_post'] = false; /* Extract the array to allow easy use of variables. */ extract( $args ); /* Check for a cached image. */ $cache = wp_cache_get( 'get_the_image' ); if ( !is_array( $cache ) ) $cache = array(); /* If there is no cached image, let's see if one exists. */ if ( !isset( $cache[$post_id][$size] ) ) { /* If a custom field key (array) is defined, check for images by custom field. */ if ( $custom_key ) $image = image_by_custom_field( $args ); /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */ if ( !$image && $the_post_thumbnail ) $image = image_by_the_post_thumbnail( $args ); /* If no image found and $attachment is set to true, check for an image by attachment. */ if ( !$image && $attachment ) $image = image_by_attachment( $args ); /* If no image found and $image_scan is set to true, scan the post for images. */ if ( !$image && $image_scan ) $image = image_by_scan( $args ); /* If no image found and a $default_image is set, get the default image. */ if ( !$image && $default_image ) $image = image_by_default( $args ); /* If an image is returned, run it through the display function. */ if ( $image ) $image = display_the_image( $args, $image ); $cache[$post_id][$size] = $image; wp_cache_set( 'get_the_image', $cache ); } else { $image = $cache[$post_id][$size]; } /* Allow plugins/theme to override the final output. */ $image = apply_filters( 'get_the_image', $image ); /* Display the image if $echo is set to true and the $format isn't an array. Else, return the image. */ if ( 'array' == $format ) { $atts = wp_kses_hair( $image, array( 'http' ) ); foreach ( $atts as $att ) $out[$att['name']] = $att['value']; $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'. return $out; } elseif ( $echo ) echo $image; else return $image; }
/** * The main image function for displaying an image. It supports several arguments that allow developers to * customize how the script outputs the image. * * The image check order is important to note here. If an image is found by any specific check, the script * will no longer look for images. The check order is 'meta_key', 'the_post_thumbnail', 'attachment', * 'image_scan', 'callback', and 'default_image'. * * @since 0.1.0 * @global $post The current post's DB object. * @param array $args Arguments for how to load and display the image. * @return string|array The HTML for the image. | Image attributes in an array. */ function get_the_image( $args = array() ) { global $post; /* Set the default arguments. */ $defaults = array( 'meta_key' => array( 'Thumbnail', 'thumbnail' ), 'post_id' => $post->ID, 'attachment' => true, 'the_post_thumbnail' => true, // WP 2.9+ image function 'size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'meta_key_save' => false, 'callback' => null, 'cache' => true, 'echo' => true, 'custom_key' => null, // @deprecated 0.6. Use 'meta_key'. 'default_size' => null, // @deprecated 0.5. Use 'size'. ); /* Allow plugins/themes to filter the arguments. */ $args = apply_filters( 'get_the_image_args', $args ); /* Merge the input arguments and the defaults. */ $args = wp_parse_args( $args, $defaults ); /* If $default_size is given, overwrite $size. */ if ( !is_null( $args['default_size'] ) ) $args['size'] = $args['default_size']; // Deprecated 0.5 in favor of $size /* If $custom_key is set, overwrite $meta_key. */ if ( !is_null( $args['custom_key'] ) ) $args['meta_key'] = $args['custom_key']; // Deprecated 0.6 in favor of $meta_key /* If $format is set to 'array', don't link to the post. */ if ( 'array' == $args['format'] ) $args['link_to_post'] = false; /* Extract the array to allow easy use of variables. */ extract( $args ); /* Get cache key based on $args. */ $key = md5( serialize( compact( array_keys( $args ) ) ) ); /* Check for a cached image. */ $image_cache = wp_cache_get( $post_id, 'get_the_image' ); if ( !is_array( $image_cache ) ) $image_cache = array(); /* If there is no cached image, let's see if one exists. */ if ( !isset( $image_cache[$key] ) || empty( $cache ) ) { /* If a custom field key (array) is defined, check for images by custom field. */ if ( !empty( $meta_key ) ) $image = image_by_custom_field( $args ); /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */ if ( empty( $image ) && !empty( $the_post_thumbnail ) ) $image = image_by_the_post_thumbnail( $args ); /* If no image found and $attachment is set to true, check for an image by attachment. */ if ( empty( $image ) && !empty( $attachment ) ) $image = image_by_attachment( $args ); /* If no image found and $image_scan is set to true, scan the post for images. */ if ( empty( $image ) && !empty( $image_scan ) ) $image = image_by_scan( $args ); /* If no image found and a callback function was given. */ if ( empty( $image ) && !is_null( $callback ) && function_exists( $callback ) ) $image = call_user_func( $callback, $args ); /* If no image found and a $default_image is set, get the default image. */ if ( empty( $image ) && !empty( $default_image ) ) $image = image_by_default( $args ); /* If an image was found. */ if ( !empty( $image ) ) { /* Change $image['url'] to $image['src']. */ if ( isset( $image['url'] ) ) // 'url' key deprecated 0.6.2 $image['src'] = $image['url']; /* If $meta_key_save was set, save the image to a custom field. */ if ( !empty( $meta_key_save ) ) get_the_image_meta_key_save( $args, $image ); /* If an image is returned, run it through the display function. */ if ( !empty( $image ) ) $image = display_the_image( $args, $image ); } $image_cache[$key] = $image; wp_cache_set( $post_id, $image_cache, 'get_the_image' ); } else { $image = $image_cache[$key]; } /* Allow plugins/theme to override the final output. */ $image = apply_filters( 'get_the_image', $image ); /* If $format is set to 'array', return an array of image attributes. */ if ( 'array' == $format ) { $atts = wp_kses_hair( $image, array( 'http' ) ); foreach ( $atts as $att ) $out[$att['name']] = $att['value']; $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'. return $out; } /* Or, if $echo is set to false, return the formatted image. */ elseif ( empty( $echo ) ) { return $image; } /* Display the image if we get to this point. */ echo $image; }