标头中的条件背景图片 [英] Conditional background image in header
问题描述
我正在尝试修改网站的内容标题,因此它将为搜索结果页面和帖子类型的单个帖子显示自定义背景图片.这是当前的工作代码:
I'm trying to modify the content-header of a website, so it will display a custom background image for the search result page and for single posts of a post type. Here's the current working code:
<div class="single__header" <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo 'style="background-image: url(\'';
the_post_thumbnail_url('post-background');
echo '\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>>
这就是我想出的:
<div class="single__header"
<?php if (is_page_template('single-perspactive-funding.php')) {
echo 'style="background-image: url([website-url-hidden]/content/uploads/2018/04/single-funding-header.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>
<?php elseif (is_search()) {
echo 'style="background-image: url([website-url-hidden]/content/uploads/2018/04/Header-image_Notfound.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>
<?php else : ?>
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
echo 'style="background-image: url(\'';
the_post_thumbnail_url('post-background');
echo '\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
}
?>
<?php endif; ?>>
但是由于某种原因,这无法正常工作,页眉和页面的其余部分不再加载.
But for some reason this isn't working, the header and the rest of the page is no longer loading.
我可能做错了什么,但不知道到底是什么.因此,任何帮助表示赞赏.
I'm probably doing something wrong, but don't know exactly what. So any help is appreciated.
也许是因为单一资助是职位类型而不是页面类型?所以也许我必须更改'if( is_page_template ('single-perpactive-funding. php'))'插入其他内容?
Maybe it's because the single funding is a post type and not a page? So maybe I have to change 'if (is_page_template('single-perspactive-funding.php'))' into something else?
推荐答案
语法有问题,建议在开发阶段启用错误打印. 您的代码类似于
There is an issue with your syntax, it is recommended to enable error printing in the development phase. Your code is something like
<?php
// Some code ...
<?php
// you cannot have php inside php
?>
?>
我试图在下面的代码中修复语法错误
I have tried to fix the syntactical errors in below code
<div class="single__header"
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$bgsearch = 'style="background-image: url([website-url]/content/uploads/2018/04/Header-image_Notfound.jpg); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
$bgdefault = 'style="background-image: url(\''.the_post_thumbnail_url('post-background').'\'); background-repeat: no-repeat; background-position: center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"';
if (is_search()) {
echo $bgsearch;
} else {
echo $bgdefault;
}
}
?>
>
Div contents
</div>
希望这会有所帮助.
这篇关于标头中的条件背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!