获得中等大小而不是完整
get medium size instead of full
我正在努力为 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 docs,函数returns一个数组。数组的第一个元素是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; ?>); ">
我正在努力为 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 docs,函数returns一个数组。数组的第一个元素是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; ?>); ">