如何将 Img Post 缩略图设置为整个 post 的背景

How Can i set the Img Post Thumbnail as a background of whole post

任何人都可以帮助我制作我的 wp 循环的完整背景 posts 是 post

的特色图片的缩略图

enter image description here

在您的 header.phpsingle.php 文件中(无论您的开头 <body> 元素在哪里)您可以执行以下操作:

<body style="background: url('<?php echo get_the_post_thumbnail_url($post->ID, 'thumbnail'); ?>') no-repeat center center;">

或者您可以将 post 包装在新的包装器 div 中,并以相同的方式设置背景。 注意 您必须在循环内才能正确使用 $post 对象,或者您可以在您尝试设置的文件顶部调用 global $post;在。

如果您希望使用上传的特色图片的 完整 版本,只需将 get_the_post_thumbnail_url() 中的 thumbnail 参数更改为 full ] 函数。

参考:https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

您可以通过 post id 在 wp loop 中获取特色图片 url 并将特色图片作为背景图片添加到主 div

<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID, 'single-post-thumbnail' ); ?>
  <div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')"></div>