本文整理汇总了PHP中wp_image_add_srcset_and_sizes函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_image_add_srcset_and_sizes函数的具体用法?PHP wp_image_add_srcset_and_sizes怎么用?PHP wp_image_add_srcset_and_sizes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_image_add_srcset_and_sizes函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: inti_get_srcset_image
/**
* Add SRCSET to non-content images
* Make images uploaded in Customizer or Theme Options,
* that don't pass through the WP 4.4 filter responsive
*
* @package Inti
* @since 1.0.0
* @license GNU General Public License v2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
*/
function inti_get_srcset_image($url, $alt)
{
$attachment_id = inti_get_attachment_id($url);
$img_html = '';
$img_html .= '<img src="' . $url . '" alt="' . $alt . '">';
if ($url == "") {
return "";
}
if ($attachment_id == 0) {
// D'oh - Just return the non-responsive URL
return $img_html;
} else {
if (function_exists('wp_image_add_srcset_and_sizes')) {
$final_image = wp_image_add_srcset_and_sizes($img_html, wp_get_attachment_metadata($attachment_id, false), $attachment_id);
return $final_image;
} else {
return $img_html;
}
}
}
示例2: wp_make_content_images_responsive
/**
* Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
*
* @since 4.4.0
*
* @see wp_image_add_srcset_and_sizes()
*
* @param string $content The raw post content to be filtered.
* @return string Converted content with 'srcset' and 'sizes' attributes added to images.
*/
function wp_make_content_images_responsive($content)
{
if (!preg_match_all('/<img [^>]+>/', $content, $matches)) {
return $content;
}
$selected_images = $attachment_ids = array();
foreach ($matches[0] as $image) {
if (false === strpos($image, ' srcset=') && preg_match('/wp-image-([0-9]+)/i', $image, $class_id) && ($attachment_id = absint($class_id[1]))) {
/*
* If exactly the same image tag is used more than once, overwrite it.
* All identical tags will be replaced later with 'str_replace()'.
*/
$selected_images[$image] = $attachment_id;
// Overwrite the ID when the same image is included more than once.
$attachment_ids[$attachment_id] = true;
}
}
if (count($attachment_ids) > 1) {
/*
* Warm object cache for use with 'get_post_meta()'.
*
* To avoid making a database call for each image, a single query
* warms the object cache with the meta information for all images.
*/
update_meta_cache('post', array_keys($attachment_ids));
}
foreach ($selected_images as $image => $attachment_id) {
$image_meta = get_post_meta($attachment_id, '_wp_attachment_metadata', true);
$content = str_replace($image, wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id), $content);
}
return $content;
}