本文整理汇总了PHP中wp_get_attachment_image_srcset函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_get_attachment_image_srcset函数的具体用法?PHP wp_get_attachment_image_srcset怎么用?PHP wp_get_attachment_image_srcset使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_get_attachment_image_srcset函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_html
public function get_html($media_id, $sizes = null, array $attrs = array(), $square = false, $def_wp_size = null, $use_fallback = false)
{
if (empty($def_wp_size)) {
$def_wp_size = $square ? 'thumbnail' : 'medium';
}
$img_src = wp_get_attachment_image_url($media_id, $def_wp_size);
if (empty($img_src)) {
$result = $use_fallback ? call_user_func_array(array($this, 'get_html_fallback'), func_get_args()) : false;
return $result;
}
$img_srcset = wp_get_attachment_image_srcset($media_id, $def_wp_size);
$img_data = array();
foreach (get_intermediate_image_sizes() as $size) {
$img_data[$size] = wp_get_attachment_image_src($media_id, $size);
}
$img_data = $this->filter_square($img_data, $square);
if (!empty($this->filter_srcset)) {
foreach ($this->filter_srcset as $filter) {
$img_srcset = call_user_func($filter, $img_data, $media_id, $size, $attrs, $square, $def_wp_size);
}
}
$attrs = array_merge(array('src' => $img_src, 'srcset' => $img_srcset, 'sizes' => $this->get_sizes($sizes)), $attrs);
if (empty($attrs['srcset'])) {
unset($attrs['srcset']);
unset($attrs['sizes']);
}
$result = \Cibulka::Base('HTML', 'img', $attrs, true);
return $result;
}
示例2: get_thumbnail_source
function get_thumbnail_source($post_id = null)
{
$id = get_post_thumbnail_id($post_id);
$src = wp_get_attachment_image_srcset($id, 'medium', true);
if (!$src) {
$src = wp_get_attachment_url($id);
$src = $src;
}
return $src;
}
示例3: file_info
/**
* Get uploaded file information
*
* @param int $file Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
*
* @return array|bool False if file not found. Array of image info on success
*/
public static function file_info($file, $args = array())
{
if (!($path = get_attached_file($file))) {
return false;
}
$args = wp_parse_args($args, array('size' => 'thumbnail'));
list($src) = wp_get_attachment_image_src($file, $args['size']);
$attachment = get_post($file);
$info = array('ID' => $file, 'name' => basename($path), 'path' => $path, 'url' => $src, 'full_url' => wp_get_attachment_url($file), 'title' => $attachment->post_title, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'alt' => get_post_meta($file, '_wp_attachment_image_alt', true));
if (function_exists('wp_get_attachment_image_srcset')) {
$info['srcset'] = wp_get_attachment_image_srcset($file);
}
return wp_parse_args($info, wp_get_attachment_metadata($file));
}
示例4: tevkori_filter_attachment_image_attributes
/**
* Filter to add 'srcset' and 'sizes' attributes to post thumbnails and gallery images.
* The filter is added to the hook in wp-tevko-core-functions.php because
* it is only needed on a version of WordPress previous to 4.4.
*
* @since 2.3.0
* @see 'wp_get_attachment_image_attributes'
*
* @return array Attributes for image.
*/
function tevkori_filter_attachment_image_attributes($attr, $attachment, $size)
{
// Set 'srcset' and 'sizes' if not already present and both were returned.
if (empty($attr['srcset'])) {
$srcset = wp_get_attachment_image_srcset($attachment->ID, $size);
$sizes = wp_get_attachment_image_sizes($attachment->ID, $size);
if ($srcset && $sizes) {
$attr['srcset'] = $srcset;
if (empty($attr['sizes'])) {
$attr['sizes'] = $sizes;
}
}
}
return $attr;
}
示例5: file_info
/**
* Get uploaded file information
*
* @param int $file Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
*
* @return array|bool False if file not found. Array of image info on success
*/
public static function file_info($file, $args = array())
{
if (!($path = get_attached_file($file))) {
return false;
}
$args = wp_parse_args($args, array('size' => 'thumbnail'));
$image = wp_get_attachment_image_src($file, $args['size']);
$attachment = get_post($file);
$info = array('ID' => $file, 'name' => basename($path), 'path' => $path, 'url' => $image[0], 'full_url' => wp_get_attachment_url($file), 'title' => $attachment->post_title, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'alt' => get_post_meta($file, '_wp_attachment_image_alt', true));
if (function_exists('wp_get_attachment_image_srcset')) {
$info['srcset'] = wp_get_attachment_image_srcset($file);
}
$info = wp_parse_args($info, wp_get_attachment_metadata($file));
// Do not overwrite width and height by returned value of image meta
$info['width'] = $image[1];
$info['height'] = $image[2];
return $info;
}
示例6: gannet_responsive_thumbnail
/**
*
*/
function gannet_responsive_thumbnail($attachment_id)
{
$src = wp_get_attachment_image_url($attachment_id, 'medium');
$srcset = wp_get_attachment_image_srcset($attachment_id, 'medium');
$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
?>
<img src="<?php
echo esc_url($src);
?>
"
class="post-thumbnail"
alt="<?php
echo esc_textarea($alt);
?>
"
srcset="<?php
echo esc_attr($srcset);
?>
"
sizes="768px, (min-width: 768px) 300px">
<?php
}
示例7: test_wp_make_content_images_responsive_schemes
function test_wp_make_content_images_responsive_schemes()
{
$image_meta = wp_get_attachment_metadata(self::$large_id);
$size_array = array((int) $image_meta['sizes']['medium']['width'], (int) $image_meta['sizes']['medium']['height']);
$srcset = sprintf('srcset="%s"', wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta));
$sizes = sprintf('sizes="%s"', wp_get_attachment_image_sizes(self::$large_id, $size_array, $image_meta));
// Build HTML for the editor.
$img = get_image_tag(self::$large_id, '', '', '', 'medium');
$img_https = str_replace('http://', 'https://', $img);
$img_relative = str_replace('http://', '//', $img);
// Manually add srcset and sizes to the markup from get_image_tag();
$respimg = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
$respimg_https = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https);
$respimg_relative = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative);
$content = '
<p>Image, http: protocol. Should have srcset and sizes.</p>
%1$s
<p>Image, http: protocol. Should have srcset and sizes.</p>
%2$s
<p>Image, protocol-relative. Should have srcset and sizes.</p>
%3$s';
$unfiltered = sprintf($content, $img, $img_https, $img_relative);
$expected = sprintf($content, $respimg, $respimg_https, $respimg_relative);
$actual = wp_make_content_images_responsive($unfiltered);
$this->assertSame($expected, $actual);
}
示例8: siteorigin_widgets_get_attachment_image_src
}
?>
<?php
$src = siteorigin_widgets_get_attachment_image_src($image, $size, $image_fallback);
$attr = array();
if (!empty($src)) {
$attr = array('src' => $src[0]);
if (!empty($src[1])) {
$attr['width'] = $src[1];
}
if (!empty($src[2])) {
$attr['height'] = $src[2];
}
if (function_exists('wp_get_attachment_image_srcset')) {
$attr['srcset'] = wp_get_attachment_image_srcset($image, $size);
}
}
$attr = apply_filters('siteorigin_widgets_image_attr', $attr, $instance, $this);
$classes = array('so-widget-image');
if (!empty($title)) {
$attr['title'] = $title;
}
if (!empty($alt)) {
$attr['alt'] = $alt;
}
?>
<div class="sow-image-container">
<?php
if (!empty($url)) {
?>
示例9: wc_get_product_attachment_props
/**
* Gets data about an attachment, such as alt text and captions.
* @since 2.6.0
* @param object|bool $product
* @return array
*/
function wc_get_product_attachment_props($attachment_id = null, $product = false)
{
$props = array('title' => '', 'caption' => '', 'url' => '', 'alt' => '', 'src' => '', 'srcset' => false, 'sizes' => false);
if ($attachment = get_post($attachment_id)) {
$props['title'] = trim(strip_tags($attachment->post_title));
$props['caption'] = trim(strip_tags($attachment->post_excerpt));
$props['url'] = wp_get_attachment_url($attachment_id);
$props['alt'] = trim(strip_tags(get_post_meta($attachment_id, '_wp_attachment_image_alt', true)));
// Large version.
$src = wp_get_attachment_image_src($attachment_id, 'large');
$props['full_src'] = $src[0];
$props['full_src_w'] = $src[1];
$props['full_src_h'] = $src[2];
// Image source.
$src = wp_get_attachment_image_src($attachment_id, 'shop_single');
$props['src'] = $src[0];
$props['src_w'] = $src[1];
$props['src_h'] = $src[2];
$props['srcset'] = function_exists('wp_get_attachment_image_srcset') ? wp_get_attachment_image_srcset($attachment_id, 'shop_single') : false;
$props['sizes'] = function_exists('wp_get_attachment_image_sizes') ? wp_get_attachment_image_sizes($attachment_id, 'shop_single') : false;
// Alt text fallbacks
$props['alt'] = empty($props['alt']) ? $props['caption'] : $props['alt'];
$props['alt'] = empty($props['alt']) ? trim(strip_tags($attachment->post_title)) : $props['alt'];
$props['alt'] = empty($props['alt']) && $product ? trim(strip_tags(get_the_title($product->ID))) : $props['alt'];
}
return $props;
}
示例10: ubik_imagery_img
function ubik_imagery_img($id = '', $size = '', $alt = '', $context = '')
{
// Return a placeholder if we have no ID to work with; this is up to the theme to style
if (empty($id)) {
return array(apply_filters('ubik_imagery_placeholder', '<div class="no-image"></div>'), 0, 0);
}
// Initialize
$html = $src = $srcset = $sizes = $width = $height = $dimensions = $attributes = '';
$class = array('ubik-img');
$size = apply_filters('ubik_imagery_size', $size);
// Custom replacement for get_image_tag(); roll your own instead of using $html = get_image_tag( $id, $alt, $title, $align, $size );
list($src, $width, $height, $is_intermediate) = image_downsize($id, $size);
// Add a styling hook for square images
if (ubik_imagery_size_is_square($size)) {
$class[] = 'square';
}
// Another size was requested but WP returned the full image
if (!$is_intermediate && $size !== 'full') {
$size = 'full';
}
// Provide an opportunity to filter the `src` attribute; here you can clear it, substitute a blank image, or do something else
$src = esc_attr(apply_filters('ubik_imagery_src', $src, $id, $size));
if (!empty($src)) {
$src = 'src="' . $src . '" ';
}
// `srcset` is easy; no filter here, just use the WordPress 4.4+ standard
$srcset = wp_get_attachment_image_srcset($id, $size);
if (!empty($srcset)) {
$srcset = 'srcset="' . esc_attr($srcset) . '" ';
}
// `sizes` is slightly more complicated; these must be set at the theme level
$sizes = ubik_imagery_sizes_attribute($size, $width, $context);
if (!empty($sizes)) {
$sizes = 'sizes="' . esc_attr($sizes) . '" ';
}
// Class
$class = implode(' ', apply_filters('ubik_imagery_img_class', $class));
if (!empty($class)) {
$class = 'class="' . esc_attr($class) . '"';
}
// Aspect ratio
$ratio = ubik_imagery_sizes_ratio($width, $height);
if (!empty($ratio)) {
$ratio = 'data-aspect-ratio="' . esc_attr($ratio) . '"';
}
// Attributes
$attributes = implode(' ', apply_filters('ubik_imagery_img_attributes', array('id' => 'id="wp-image-' . esc_attr($id) . '"', 'data-id' => 'data-id="' . esc_attr($id) . '"', 'class' => $class, 'ratio' => $ratio, 'schema' => 'itemprop="contentUrl"', 'alt' => 'alt="' . $alt . '"')));
// Return an array with the final width/height so these values can be passed up to the wrapper element for certain CSS styling tricks
return array(apply_filters('ubik_imagery_img', '<img ' . apply_filters('ubik_imagery_src_html', $src) . apply_filters('ubik_imagery_srcset_html', $srcset) . apply_filters('ubik_imagery_sizes_html', $sizes) . apply_filters('ubik_imagery_dimensions', image_hwstring($width, $height), $width, $height) . $attributes . '>'), $width, $height);
}
示例11: get_post_meta
//
// case 'video':
// $video = get_post_meta( $post_ID, 'uxr_event_flexible_content_' . $count . '_video', true);
// if ( isset($video) && !empty($video) ) :
// echo apply_filters('the_content', $video) . "\n";
// endif;
// break;
//
// Image
//
case 'fullwidth_image':
$image_ID = get_post_meta($post_ID, 'uxr_event_flexible_content_' . $count . '_fullwidth_image', true);
if (isset($image_ID) && !empty($image_ID)) {
// Image source
$img_src = wp_get_attachment_image_url($image_ID, 'large');
$img_srcset = wp_get_attachment_image_srcset($image_ID, 'large');
$img_sizes = wp_get_attachment_image_sizes($image_ID, 'large');
// Image meta
$image_meta = get_posts(array('p' => $image_ID, 'post_type' => 'attachment'));
$image_caption = $image_meta[0]->post_excerpt;
$image_more_meta = wp_get_attachment_metadata($image_ID, 'large');
echo "\t" . '</div>' . "\n";
echo '</div>' . "\n";
echo '<img src="' . esc_url($img_src) . '" srcset="' . esc_attr($img_srcset) . '" sizes="' . esc_attr($img_sizes) . '" alt="" class="uxr-asset-fullwidth" />';
echo '<div class="uxr-grid-container">' . "\n";
echo "\t" . '<div class="uxr-contrib">' . "\n";
}
break;
}
}
}
示例12: post_featured_image
/**
* Get post featured image
*
* @since 1.0.0
*
* @param array $args Array, containing size and format options.
*
* @return string
*/
public function post_featured_image(array $args = array())
{
$size = $this->image_sizes['small'];
$format = '<div style="background-image: url(\'%1$s\');"><img src="%1$s" %2$s srcset="%3$s"></div>';
if (true === isset($args['size']) && false === empty($args['size'])) {
$size = $args['size'];
}
if (true === isset($args['format']) && false === empty($args['format'])) {
$format = $args['format'];
}
$atts = '';
global $_wp_additional_image_sizes;
if (has_post_thumbnail()) {
$atts = sprintf('width="%1$s" height="%2$s"', $_wp_additional_image_sizes[$size]['width'], $_wp_additional_image_sizes[$size]['height']);
$image_url = get_the_post_thumbnail_url(null, $size);
} else {
$width = get_option("{$size}_size_w");
$height = get_option("{$size}_size_h");
$image_url = "http://fakeimg.pl/{$width}x{$height}";
}
$image_url = esc_url($image_url);
$srcset = wp_get_attachment_image_srcset(get_post_thumbnail_id(), $size);
return sprintf($format, $image_url, $atts, $srcset);
}
示例13: file_info
/**
* Get uploaded file information
*
* @param int $file_id Attachment image ID (post ID). Required.
* @param array $args Array of arguments (for size).
*
* @return array|bool False if file not found. Array of image info on success
*/
static function file_info($file_id, $args = array())
{
$args = wp_parse_args($args, array('size' => 'thumbnail'));
$img_src = wp_get_attachment_image_src($file_id, $args['size']);
if (!$img_src) {
return false;
}
$attachment = get_post($file_id);
$path = get_attached_file($file_id);
return array('ID' => $file_id, 'name' => basename($path), 'path' => $path, 'url' => $img_src[0], 'width' => $img_src[1], 'height' => $img_src[2], 'full_url' => wp_get_attachment_url($file_id), 'title' => $attachment->post_title, 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'alt' => get_post_meta($file_id, '_wp_attachment_image_alt', true), 'srcset' => wp_get_attachment_image_srcset($file_id));
}
示例14: get_post_meta
// $imageid = get_post_meta( $hero->ID, 'fauval_sliderid', true );
$imageid = get_post_meta($hero->ID, 'fauval_slider_image', true);
$slidersrc = '';
$slidersrcset = '';
if (isset($imageid) && $imageid > 0) {
$sliderimage = wp_get_attachment_image_src($imageid, 'hero');
$imgdata = fau_get_image_attributs($imageid);
$copyright = trim(strip_tags($imgdata['credits']));
$slidersrcset = wp_get_attachment_image_srcset($imageid, 'hero');
} else {
$post_thumbnail_id = get_post_thumbnail_id($hero->ID);
if ($post_thumbnail_id) {
$sliderimage = wp_get_attachment_image_src($post_thumbnail_id, 'hero');
$imgdata = fau_get_image_attributs($post_thumbnail_id);
$copyright = trim(strip_tags($imgdata['credits']));
$slidersrcset = wp_get_attachment_image_srcset($post_thumbnail_id, 'hero');
}
}
if (!$sliderimage || empty($sliderimage[0])) {
$slidersrc = '<img src="' . fau_esc_url($options['src-fallback-slider-image']) . '" width="' . $options['slider-image-width'] . '" height="' . $options['slider-image-height'] . '" alt="">';
} else {
$slidersrc = '<img src="' . fau_esc_url($sliderimage[0]) . '" width="' . $options['slider-image-width'] . '" height="' . $options['slider-image-height'] . '" alt=""';
if ($slidersrcset) {
$slidersrc .= ' srcset="' . $slidersrcset . '"';
}
$slidersrc .= '>';
}
echo $slidersrc . "\n";
if ($options['advanced_display_hero_credits'] == true && !empty($copyright)) {
echo '<p class="credits">' . $copyright . "</p>";
}
示例15: test_wp_make_content_images_responsive
/**
* @ticket 33641
*/
function test_wp_make_content_images_responsive()
{
$srcset = sprintf('srcset="%s"', wp_get_attachment_image_srcset(self::$large_id, 'medium'));
$sizes = sprintf('sizes="%s"', wp_get_attachment_image_sizes(self::$large_id, 'medium'));
// Function used to build HTML for the editor.
$img = get_image_tag(self::$large_id, '', '', '', 'medium');
$img_no_size = str_replace('size-', '', $img);
$img_no_size_id = str_replace('wp-image-', 'id-', $img_no_size);
// Manually add srcset and sizes to the markup from get_image_tag();
$respimg = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
$respimg_no_size = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size);
$content = '<p>Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference.</p>
<p>First things first:</p>
%1$s
<ul>
<li><a href="http://wordpress.org" title="Subscribe to the WordPress mailing list for Release Notifications">Subscribe to the WordPress mailing list for release notifications</a></li>
</ul>
%2$s
<p>As a subscriber, you will receive an email every time an update is available (and only then). This will make it easier to keep your site up to date, and secure from evildoers.<br />
When a new version is released, <a href="http://wordpress.org" title="If you are already logged in, this will take you directly to the Dashboard">log in to the Dashboard</a> and follow the instructions.<br />
Upgrading is a couple of clicks!</p>
%3$s
<p>Then you can start enjoying the WordPress experience:</p>
<ul>
<li>Edit your personal information at <a href="http://wordpress.org" title="Edit settings like your password, your display name and your contact information">Users › Your Profile</a></li>
<li>Start publishing at <a href="http://wordpress.org" title="Create a new post">Posts › Add New</a> and at <a href="http://wordpress.org" title="Create a new page">Pages › Add New</a></li>
<li>Browse and install plugins at <a href="http://wordpress.org" title="Browse and install plugins at the official WordPress repository directly from your Dashboard">Plugins › Add New</a></li>
<li>Browse and install themes at <a href="http://wordpress.org" title="Browse and install themes at the official WordPress repository directly from your Dashboard">Appearance › Add New Themes</a></li>
<li>Modify and prettify your website’s links at <a href="http://wordpress.org" title="For example, select a link structure like: http://example.com/1999/12/post-name">Settings › Permalinks</a></li>
<li>Import content from another system or WordPress site at <a href="http://wordpress.org" title="WordPress comes with importers for the most common publishing systems">Tools › Import</a></li>
<li>Find answers to your questions at the <a href="http://wordpress.orgs" title="The official WordPress documentation, maintained by the WordPress community">WordPress Codex</a></li>
</ul>';
$content_unfiltered = sprintf($content, $img, $img_no_size, $img_no_size_id);
$content_filtered = sprintf($content, $respimg, $respimg_no_size, $img_no_size_id);
$this->assertSame($content_filtered, wp_make_content_images_responsive($content_unfiltered));
}