我实际上已经修复了一个解决方案,这个解决方案在帖子中使用了一个自定义字段,并基于此,将背景加载到帖子中。
为了解决背景图像上不同格式的问题,我得到了(不记得名称)的帮助,编写了一段代码,它首先尝试加载背景。jpg,如果失败,它将加载后台。gif等。因此,通过将此代码添加到函数中。php,就可以了
// set background based on custom field in post
function rbpet_post_background () {
if ( empty( $background = get_post_meta( get_the_ID(), \'usp-custom-background_image\', true ) ) ) return;
$base = "https://example.com/img/backgrounds/";
$extensions = array( ".jpg" , ".gif" , ".mp4" );
foreach ( $extensions as $ext ) {
$file = $base . $background . $ext;
$file_headers = @get_headers( $file );
if ( $file_headers[0] == "HTTP/1.1 200 OK" ) {
$background_url = $file;
break;
}
}
if ( empty( $background_url ) ) return;
?>
<style type="text/css">
body { background-image: url( "<?php echo ($background_url); ?>")!important;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center center;
max-width: 100%;
height: auto;
}
</style>
<?php
}
add_action( "wp_head" , "rbpet_post_background" );