Jetpack_PostImages::get_image PHP Method

get_image() static public method

Run through the different methods that we have available to try to find a single good display image for this post.
static public get_image ( integer $post_id, array $args = [] ) : Array
$post_id integer
$args array Other arguments (currently width and height required for images where possible to determine)
return Array containing details of the best image to be used
    static function get_image($post_id, $args = array())
    {
        $image = '';
        /**
         * Fires before we find a single good image for a specific post.
         *
         * @since 2.2.0
         *
         * @param int $post_id Post ID.
         */
        do_action('jetpack_postimages_pre_get_image', $post_id);
        $media = self::get_images($post_id, $args);
        if (is_array($media)) {
            foreach ($media as $item) {
                if ('image' == $item['type']) {
                    $image = $item;
                    break;
                }
            }
        }
        /**
         * Fires after we find a single good image for a specific post.
         *
         * @since 2.2.0
         *
         * @param int $post_id Post ID.
         */
        do_action('jetpack_postimages_post_get_image', $post_id);
        return $image;
    }

Usage Example

/**
 * Get an image from a post
 *
 * @uses Jetpack_PostImages::get_image( $post_id ) to get the source of an image in a post, apply_filters()
 *
 * @since 1.0
 *
 * @return string $the_image the image source
 */
function colorposts_get_post_image()
{
    $post_id = get_the_ID();
    if (class_exists('Jetpack_PostImages')) {
        $the_image = Jetpack_PostImages::get_image($post_id);
        if (!empty($the_image['src'])) {
            $the_image = $the_image['src'];
        } else {
            $the_image = apply_filters('jetpack_open_graph_image_default', "http://wordpress.com/i/blank.jpg");
        }
    }
    $the_image = apply_filters('colorposts_image_output', $the_image);
    return esc_url($the_image);
}
All Usage Examples Of Jetpack_PostImages::get_image