$post_id ? $post_id : get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', ) ) ); if ( isset( $image_ids[$index] ) ) { return $image_ids[$index]; } return false; } /** * Return an image pulled from the media gallery. * * @since 1.0.0 */ function bizznis_get_image( $args = array() ) { $defaults = array( 'post_id' => null, 'format' => 'html', 'size' => 'full', 'num' => 0, 'attr' => '', 'fallback' => 'first-attached', 'context' => '', ); /** * A filter on the default parameters used by `bizznis_get_image()`. * * @since unknown */ $defaults = apply_filters( 'bizznis_get_image_default_args', $defaults, $args ); $args = wp_parse_args( $args, $defaults ); # Allow child theme to short-circuit this function $pre = apply_filters( 'bizznis_pre_get_image', false, $args, get_post() ); if ( false !== $pre ) { return $pre; } # If post thumbnail (native WP) exists, use its id if ( has_post_thumbnail( $args['post_id'] ) && ( 0 === $args['num'] ) ) { $id = get_post_thumbnail_id( $args['post_id'] ); } # Else if the first (default) image attachment is the fallback, use its id elseif ( 'first-attached' === $args['fallback'] ) { $id = bizznis_get_image_id( $args['num'], $args['post_id'] ); } # Else if fallback id is supplied, use it elseif ( is_int( $args['fallback'] ) ) { $id = $args['fallback']; } # If we have an id, get the html and url if ( isset( $id ) ) { $html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] ); list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] ); } # Else if fallback html and url exist, use them elseif ( is_array( $args['fallback'] ) ) { $id = 0; $html = $args['fallback']['html']; $url = $args['fallback']['url']; } # Else, return false (no image) else { return false; } # Source path, relative to the root $src = str_replace( home_url(), '', $url ); # Determine output if ( 'html' === strtolower( $args['format'] ) ) { $output = $html; } elseif ( 'url' === strtolower( $args['format'] ) ) { $output = $url; } else { $output = $src; } # Return false if $url is blank if ( empty( $url ) ) { $output = false; } # Return data, filtered return apply_filters( 'bizznis_get_image', $output, $args, $id, $html, $url, $src ); } /** * Echo an image pulled from the media gallery. * * @since 1.0.0 */ function bizznis_image( $args = array() ) { /* Supported $args keys are: - format - string, default is 'html', may be 'url' - size - string, default is 'full' - num - integer, default is 0 - attr - string, default is '' */ $image = bizznis_get_image( $args ); if ( $image ) { echo $image; } else { return false; } } /** * Return registered image sizes. Return two-dimensional, with width, height and crop sub-keys. * * @since 1.0.0 */ function bizznis_get_additional_image_sizes() { global $_wp_additional_image_sizes; if ( $_wp_additional_image_sizes ) { return $_wp_additional_image_sizes; } return array(); } /** * Return all registered image sizes arrays, including the standard sizes. Return two-dimensional, with width, height and crop sub-keys. * * @since 1.0.0 */ function bizznis_get_image_sizes() { $builtin_sizes = array( 'large' => array( 'width' => get_option( 'large_size_w' ), 'height' => get_option( 'large_size_h' ), ), 'medium' => array( 'width' => get_option( 'medium_size_w' ), 'height' => get_option( 'medium_size_h' ), ), 'thumbnail' => array( 'width' => get_option( 'thumbnail_size_w' ), 'height' => get_option( 'thumbnail_size_h' ), 'crop' => get_option( 'thumbnail_crop' ), ), ); $additional_sizes = bizznis_get_additional_image_sizes(); return array_merge( $builtin_sizes, $additional_sizes ); } function bizznis_get_image_sizes_for_customizer() { $sizes = array(); foreach ( (array) bizznis_get_image_sizes() as $name => $size ) { $sizes[ $name ] = $name . ' (' . absint( $size['width'] ) . ' × ' . absint( $size['height'] ) . ')'; } return $sizes; }