我正在努力获取Wordpress中图像的URL的中等大小。我的工作来源如下所示:
<div class="entry-thumbnail coverback" style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>); ">
但这需要完整图片,并且由于上传的完整图片更大,因此我的页面每次加载会爆炸到9-10 MB。
因此,到目前为止,我已经尝试过但没有成功:
<div class="entry-thumbnail coverback" style="background-image: url(<?php echo $image_attr = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); ?>); ">
和
<div class="entry-thumbnail coverback" style="background-image: url(<?php echo wp_get_attachment_image ( int $attachment_id, string|array $size = 'medium', bool $icon = false, string|array $attr = '' ); ?>); ">
和
<div class="entry-thumbnail coverback" style="background-image: url(<?php echo wp_get_attachment_image( get_the_ID(), array('medium') ); ?>); ">
但其中没有一个工作,我在这里做错了什么?我试图使用WP Codex和其他东西,但是..大多数使我感到困惑的是,它没有帮助:(
任何帮助将非常感激。
该函数从wp_get_attachment_image_src文档返回一个数组。数组中的第一个元素是src url,因此您需要按以下方式修改代码:
<?php
// Load the attachment attributes
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');
// IF it's an array, and the url (index 0) is set, then assign it
$image = (isset($image[0])) ? $image[0] : '';
?>
<div class="entry-thumbnail coverback" style="background-image: url(<?php echo $image; ?>); ">
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句