Example #1
0
/**
 * Return an image pulled from the media gallery.
 *
 * Supported $args keys are:
 *
 *  - format   - string, default is 'html'
 *  - size     - string, default is 'full'
 *  - num      - integer, default is 0
 *  - attr     - string, default is ''
 *  - fallback - mixed, default is 'first-attached'
 *
 * Applies `genesis_get_image_default_args`, `genesis_pre_get_image` and `genesis_get_image` filters.
 *
 * @since 0.1.0
 *
 * @param array|string $args Optional. Image query arguments. Default is empty array.
 * @return string|bool Return image element HTML, URL of image, or `false`.
 */
function genesis_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 `genesis_get_image()`.
     *
     * @since unknown
     */
    $defaults = apply_filters('genesis_get_image_default_args', $defaults, $args);
    $args = wp_parse_args($args, $defaults);
    // Allow child theme to short-circuit this function.
    $pre = apply_filters('genesis_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']);
    } elseif ('first-attached' === $args['fallback']) {
        // Else if the first (default) image attachment is the fallback, use its id.
        $id = genesis_get_image_id($args['num'], $args['post_id']);
    } elseif (is_int($args['fallback'])) {
        // Else if fallback id is supplied, use it.
        $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']);
    } elseif (is_array($args['fallback'])) {
        // Else if fallback HTML and URL exist, use them.
        $id = 0;
        $html = $args['fallback']['html'];
        $url = $args['fallback']['url'];
    } else {
        // No image.
        return false;
    }
    // Source path, relative to the root.
    $src = str_replace(home_url(), '', $url);
    // Determine output.
    if ('html' === mb_strtolower($args['format'])) {
        $output = $html;
    } elseif ('url' === mb_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('genesis_get_image', $output, $args, $id, $html, $url, $src);
}
Example #2
0
/**
 * Return an image pulled from the media gallery.
 *
 * Supported $args keys are:
 *
 *  - format   - string, default is 'html'
 *  - size     - string, default is 'full'
 *  - num      - integer, default is 0
 *  - attr     - string, default is ''
 *  - fallback - mixed, default is 'first-attached'
 *
 * Applies `genesis_get_image_default_args`, `genesis_pre_get_image` and `genesis_get_image` filters.
 *
 * @since 0.1.0
 *
 * @uses genesis_get_image_id() Pull an attachment ID from a post, if one exists.
 *
 * @param array|string $args Optional. Image query arguments. Default is empty array.
 *
 * @return string|boolean Return image element HTML, URL of image, or false.
 */
function genesis_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 `genesis_get_image()`.
     *
     * @since unknown
     */
    $defaults = apply_filters('genesis_get_image_default_args', $defaults, $args);
    $args = wp_parse_args($args, $defaults);
    //* Allow child theme to short-circuit this function
    $pre = apply_filters('genesis_pre_get_image', false, $args, get_post());
    if (false !== $pre) {
        return $pre;
    }
    //* Check for post image (native WP)
    if (has_post_thumbnail($args['post_id']) && 0 === $args['num']) {
        $id = get_post_thumbnail_id($args['post_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']);
    } elseif ('first-attached' === $args['fallback']) {
        $id = genesis_get_image_id($args['num'], $args['post_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']);
    } elseif (is_array($args['fallback'])) {
        $id = 0;
        $html = $args['fallback']['html'];
        $url = $args['fallback']['url'];
    } else {
        return false;
    }
    //* Source path, relative to the root
    $src = str_replace(home_url(), '', $url);
    //* Determine output
    if ('html' === mb_strtolower($args['format'])) {
        $output = $html;
    } elseif ('url' === mb_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('genesis_get_image', $output, $args, $id, $html, $url, $src);
}
Example #3
0
/**
 * Return an image pulled from the media gallery.
 *
 * Supported $args keys are:
 *
 *  - format   - string, default is 'html'
 *  - size     - string, default is 'full'
 *  - num      - integer, default is 0
 *  - attr     - string, default is ''
 *  - fallback - mixed, default is 'first-attached'
 *
 * Applies `genesis_get_image_default_args`, `genesis_pre_get_image` and `genesis_get_image` filters.
 *
 * @since 0.1.0
 *
 * @uses genesis_get_image_id() Pull an attachment ID from a post, if one exists.
 *
 * @global WP_Post $post Post object.
 *
 * @param array|string $args Optional. Image query arguments. Default is empty array.
 *
 * @return string|boolean Return image element HTML, URL of image, or false.
 */
function genesis_get_image($args = array())
{
    global $post;
    $defaults = apply_filters('genesis_get_image_default_args', array('format' => 'html', 'size' => 'full', 'num' => 0, 'attr' => '', 'fallback' => 'first-attached', 'context' => ''));
    $args = wp_parse_args($args, $defaults);
    //* Allow child theme to short-circuit this function
    $pre = apply_filters('genesis_pre_get_image', false, $args, $post);
    if (false !== $pre) {
        return $pre;
    }
    //* Check for post image (native WP)
    if (has_post_thumbnail() && 0 === $args['num']) {
        $id = get_post_thumbnail_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']);
    } elseif ('first-attached' === $args['fallback']) {
        $id = genesis_get_image_id($args['num']);
        $html = wp_get_attachment_image($id, $args['size'], false, $args['attr']);
        list($url) = wp_get_attachment_image_src($id, $args['size'], false, $args['attr']);
    } elseif (is_array($args['fallback'])) {
        $id = 0;
        $html = $args['fallback']['html'];
        $url = $args['fallback']['url'];
    } else {
        return false;
    }
    //* Source path, relative to the root
    $src = str_replace(home_url(), '', $url);
    //* Determine output
    if ('html' === mb_strtolower($args['format'])) {
        $output = $html;
    } elseif ('url' === mb_strtolower($args['format'])) {
        $output = $url;
    } else {
        $output = $src;
    }
    // Return false if $url is blank
    if (empty($url)) {
        $output = false;
    }
    //* Return false if $src is invalid (file doesn't exist)
    //	if ( ! file_exists( ABSPATH . $src ) )
    //		$output = false;
    //* Return data, filtered
    return apply_filters('genesis_get_image', $output, $args, $id, $html, $url, $src);
}
function genesis_get_image($args = array())
{
    global $post;
    $defaults = array('format' => 'html', 'size' => 'full', 'num' => 0, 'attr' => '');
    $defaults = apply_filters('genesis_get_image_default_args', $defaults);
    $args = wp_parse_args($args, $defaults);
    // Allow child theme to short-circuit this function
    $pre = apply_filters('genesis_pre_get_image', false, $args, $post);
    if (false !== $pre) {
        return $pre;
    }
    // check for post image (native WP)
    if (has_post_thumbnail() && $args['num'] === 0) {
        $id = get_post_thumbnail_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 {
        $id = genesis_get_image_id($args['num']);
        $html = wp_get_attachment_image($id, $args['size'], false, $args['attr']);
        list($url) = wp_get_attachment_image_src($id, $args['size'], false, $args['attr']);
    }
    // source path, relative to the root
    $src = str_replace(get_bloginfo('url'), '', $url);
    // determine output
    if (strtolower($args['format']) == 'html') {
        $output = $html;
    } elseif (strtolower($args['format']) == 'url') {
        $output = $url;
    } else {
        $output = $src;
    }
    // return FALSE if $url is blank
    if (empty($url)) {
        $output = FALSE;
    }
    // return FALSE if $src is invalid (file doesn't exist)
    //if ( !file_exists(ABSPATH . $src) ) $output = FALSE;
    // return data, filtered
    return apply_filters('genesis_get_image', $output, $args, $id, $html, $url, $src);
}
/**
 * @deprecated in 0.1.6
 *
 */
function sp_get_image_id($num = 0)
{
    return genesis_get_image_id($num);
}